小编Don*_*yen的帖子

在标签中发出获取字符串的行数组

我有一个字符串:“制作牙膏和漱口水的想法”。

我使用波纹管代码来获取 label 上的最后一个字符串。结果字符串最后一行:“和漱口水”。但是标签显示的最后一行:“牙膏和漱口水”如何在标签上准确获取最后一行的字符串?这是我的代码:

-(NSArray *)getLinesArrayOfStringInLabel:(UILabel *)label 
{ 
    NSString *text = [label text]; 
    UIFont *font = [label font]; 
    CGRect rect = [label bounds]; 
    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 …
Run Code Online (Sandbox Code Playgroud)

label objective-c ios

0
推荐指数
1
解决办法
540
查看次数

标签 统计

ios ×1

label ×1

objective-c ×1