计算UILabel中可见文本的范围

我有一个固定大小的UILabel ,我在这个UILabel设置的文本长度可以是UILabel或500个字符。 我想要做的是计算我可以用当前的UILabel大小将多少可见文本放入此UILabel

为什么我要这样做? 因为我想添加一个...Read more在文本末尾...Read more文本,但不是在整个文本的末尾,只是在UILabel可见文本的末尾。

提前致谢。

所以我创建了一个方法,它返回当前可见的字符串高度(具有UITextView / UITextField或UILabel的大小),并且它也支持iOS6 +,这就是我所做的:

 - (NSUInteger)fitString:(NSString *)string intoLabel:(UILabel *)label { UIFont *font = label.font; NSLineBreakMode mode = label.lineBreakMode; CGFloat labelWidth = label.frame.size.width; CGFloat labelHeight = label.frame.size.height; CGSize sizeConstraint = CGSizeMake(labelWidth, CGFLOAT_MAX); if (SYSTEM_VERSION_GREATER_THAN(iOS_7)) { NSDictionary *attributes = @{ NSFontAttributeName : font }; NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:string attributes:attributes]; CGRect boundingRect = [attributedText boundingRectWithSize:sizeConstraint options:NSStringDrawingUsesLineFragmentOrigin context:nil]; { if (boundingRect.size.height > labelHeight) { NSUInteger index = 0; NSUInteger prev; NSCharacterSet *characterSet = [NSCharacterSet whitespaceAndNewlineCharacterSet]; do { prev = index; if (mode == NSLineBreakByCharWrapping) index++; else index = [string rangeOfCharacterFromSet:characterSet options:0 range:NSMakeRange(index + 1, [string length] - index - 1)].location; } while (index != NSNotFound && index < [string length] && [[string substringToIndex:index] boundingRectWithSize:sizeConstraint options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.height <= labelHeight); return prev; } } } else { if ([string sizeWithFont:font constrainedToSize:sizeConstraint lineBreakMode:mode].height > labelHeight) { NSUInteger index = 0; NSUInteger prev; NSCharacterSet *characterSet = [NSCharacterSet whitespaceAndNewlineCharacterSet]; do { prev = index; if (mode == NSLineBreakByCharWrapping) index++; else index = [string rangeOfCharacterFromSet:characterSet options:0 range:NSMakeRange(index + 1, [string length] - index - 1)].location; } while (index != NSNotFound && index < [string length] && [[string substringToIndex:index] sizeWithFont:font constrainedToSize:sizeConstraint lineBreakMode:mode].height <= labelHeight); return prev; } } return [string length]; } 

当然,SYSTEM_VERSION_GREATER_THAN(iOS_7)都是我定义的宏。 你也应该定义自己的。

祝你好运!

尝试在UILabel中使用以下方法

像这样设置你的文字

在字符串中添加文本,如: – > lbl.text = @“abcdeedjedaekd …..阅读更多。”

它是一个理念不合适的解决方案,但可能对您有所帮助

您可以设置固定数量的字符,后跟:

 CGSize size = [string sizeWithFont:font constrainedToSize:sizeConstraint lineBreakMode:UILineBreakModeWordWrap]; 

并根据特定字体的文本长度找出UILabelCGSize多少并追加。 阅读更多固定数量的字符。 这种转变的原因是在截断之前没有找到标签中可见字符数的本地方法。

这个对我有用,希望它对你有帮助,

 - (NSUInteger)fitString:(NSString *)string intoLabel:(UILabel *)label { UIFont *font = label.font; NSLineBreakMode mode = label.lineBreakMode; CGFloat labelWidth = label.frame.size.width; CGFloat labelHeight = label.frame.size.height; CGSize sizeConstraint = CGSizeMake(labelWidth, CGFLOAT_MAX); NSDictionary *attributes = @{ NSFontAttributeName : font }; NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:string attributes:attributes]; CGRect boundingRect = [attributedText boundingRectWithSize:sizeConstraint options:NSStringDrawingUsesLineFragmentOrigin context:nil]; { if (boundingRect.size.height > labelHeight) { NSUInteger index = 0; NSUInteger prev; NSCharacterSet *characterSet = [NSCharacterSet whitespaceAndNewlineCharacterSet]; do { prev = index; if (mode == NSLineBreakByCharWrapping) index++; else index = [string rangeOfCharacterFromSet:characterSet options:0 range:NSMakeRange(index + 1, [string length] - index - 1)].location; } while (index != NSNotFound && index < [string length] && [[string substringToIndex:index] boundingRectWithSize:sizeConstraint options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.height <= labelHeight); return prev; } } return [string length]; } 
Interesting Posts