加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

获得多行UILabel的每一行TEXT(获取UILabel最后一行文字)

(2014-06-17 18:43:40)
标签:

multiline

uilabel

lastline

width

position

分类: 技术iOS
网上比较多的做法如下:

if (label.lineBreakMode != NSLineBreakByCharWrapping) {
       return nil;
   }

   NSMutableArray *linesArr = [[NSMutableArray alloc] init];
   NSCharacterSet *wordSeparators = [NSCharacterSet whitespaceAndNewlineCharacterSet];

   NSString *currentLineText = label.text;
   int textLength = label.text.length;

   NSRange rCurrentLineRange = NSMakeRange(0, textLength);
   NSRange rWhiteSpace = NSMakeRange(0, 0);
   NSRange rRemainingText = NSMakeRange(0, textLength);

   BOOL done = NO;
   while (!done) {
       rWhiteSpace.location = rWhiteSpace.location + rWhiteSpace.length;
       rWhiteSpace.length = textLength - rWhiteSpace.location;
       rWhiteSpace = [label.text rangeOfCharacterFromSet:wordSeparators options:NSCaseInsensitiveSearch range:rWhiteSpace];
       if (rWhiteSpace.location == NSNotFound) {
           rWhiteSpace.location = textLength;
           done = YES;
       }

       NSRange rTextRange = NSMakeRange(rRemainingText.location, rWhiteSpace.location - rRemainingText.location);
       NSString *textSubString = [label.text substringWithRange:rTextRange];
       CGSize sizeText = [textSubString sizeWithFont:label.font forWidth:200 lineBreakMode:NSLineBreakByCharWrapping];
       if (sizeText.width > label.bounds.size.width) {
           [linesArr addObject:[currentLineText stringByTrimmingCharactersInSet:wordSeparators]];
           rRemainingText.location = rCurrentLineRange.location + rCurrentLineRange.length;
           rRemainingText.length = textLength - rRemainingText.location;
           continue;
       }

       rCurrentLineRange = rTextRange;
       currentLineText = textSubString;
   }
   [linesArr addObject:[currentLineText stringByTrimmingCharactersInSet:wordSeparators]];

   return linesArr;
}

核心思想就是找UILabel text的换行符。但这个方法有明显的问题,如果text的内容是中文的话它运行的很棒。倘若,text是中文的,它就无能为力了,跟踪代码,发现它在找换行符时候失败,具体应该是API实现的问题。

这里有个更好的方法,支持中英文都没问题,使用的是CoreText处理,需要引入CoreText框架,并且 #import

+ (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 (NSArray *)linesArray;
}

用这个方法就可以得到UILabel的最后一行文字,那样就可以计算得UILabel最后一行文字的宽度,就可以定位到最后一个文字的位置。这样的话,就可以应付变态的产品经理了,比如可以在文字的后面紧跟着贴一个icon什么的了。

0

阅读 收藏 喜欢 打印举报/Report
前一篇:适应
后一篇:最近
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有