objective-c - Find a way to display a text that exceeds N lines... See more.
为情所困
为情所困 2017-05-02 09:35:17
0
1
557

RT
I have implemented it, but there is a problem on 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];
}

But there is a problem with this on ios 10, please pray to God

为情所困
为情所困

reply all(1)
巴扎黑

Just use css style directly
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2; /Number of lines/
text-overflow: ellipsis;
overflow : hidden;
Not original, take it without thanks

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template