美文网首页iOS旅途
iOS超过一定行数的label在末尾加上“...更多”

iOS超过一定行数的label在末尾加上“...更多”

作者: 朝阳小麦 | 来源:发表于2018-08-15 15:12 被阅读0次

适合人群:iOS开发人员。
本文内容:获取UILabel每一行显示的文本。通过计算,在需要最后一行加上“...更多”。

1.先展示下实现效果,如下图所示:

1.PNG

2.例子为新建一个项目,在自带文件ViewController界面中改动。

代码如下:

  • ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController

@end
  • ViewController.m
#import "ViewController.h"
#import <CoreText/CoreText.h>

#define SCREENWIDTH [UIScreen mainScreen].bounds.size.width
#define SCREENHEIGHT [UIScreen mainScreen].bounds.size.height

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15.0, 100.0, SCREENWIDTH-30.0, 100.0)];
    label.font = [UIFont systemFontOfSize:16.0];
    label.numberOfLines = 4;
    [self.view addSubview:label];
    
    NSString *text = @"明月几时有?把酒问青天。不知天上宫阙,今夕是何年。我欲乘风归去,又恐琼楼玉宇,高处不胜寒。起舞弄清影,何似在人间?转朱阁,低绮户,照无眠。不应有恨,何事长向别时圆?人有悲欢离合,月有阴晴圆缺,此事古难全。但愿人长久,千里共婵娟。";
    
    //获取每行显示的文本
    label.text = text;
    NSArray *array = [self getSeparatedLinesFromLabel:label];
    NSLog(@"...%@", array);
    
    //组合需要显示的文本
    NSString *line4String = array[3];
    NSString *showText = [NSString stringWithFormat:@"%@%@%@%@...更多>", array[0], array[1], array[2], [line4String substringToIndex:line4String.length-5]];
    
    //设置label的attributedText
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:showText attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.0], NSForegroundColorAttributeName:[UIColor blackColor]}];
    [attStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.0], NSForegroundColorAttributeName:[UIColor redColor]} range:NSMakeRange(showText.length-3, 3)];
    label.attributedText = attStr;
    
}

- (NSArray *)getSeparatedLinesFromLabel:(UILabel *)label
{
    NSString *text = [label text];
    UIFont   *font = [label font];
    CGRect    rect = [label frame];
    CTFontRef myFont = CTFontCreateWithName((__bridge CFStringRef)([font fontName]), [font pointSize], NULL);
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
    [attStr addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)myFont range:NSMakeRange(0, attStr.length)];
    
    CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attStr);
    
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, CGRectMake(0,0,rect.size.width,100000));
    
    CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL);
    
    NSArray *lines = (__bridge NSArray *)CTFrameGetLines(frame);
    NSMutableArray *linesArray = [[NSMutableArray alloc]init];
    
    for (id line in lines)
    {
        CTLineRef lineRef = (__bridge CTLineRef )line;
        CFRange lineRange = CTLineGetStringRange(lineRef);
        NSRange range = NSMakeRange(lineRange.location, lineRange.length);
        
        NSString *lineString = [text substringWithRange:range];
        [linesArray addObject:lineString];
    }
    return linesArray;
    
}

完成。具体需求再改动就不难了。

相关文章

网友评论

本文标题:iOS超过一定行数的label在末尾加上“...更多”

本文链接:https://www.haomeiwen.com/subject/srrwbftx.html