搜索

首页  >  问答  >  正文

objective-c - 求一个文字超过N行,显示...查看更多的方法。

RT
我实现出来了,但是,在ios 10上面有问题。

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];
}

但是这样在ios 10上面有问题,跪求大神

为情所困为情所困2757 天前537

全部回复(1)我来回复

  • 巴扎黑

    巴扎黑2017-05-02 09:37:17

    直接用css样式就可以
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2; /行数/
    text-overflow: ellipsis;
    overflow: hidden;
    不是原创,拿去不谢

    回复
    0
  • 取消回复