计算动态UILabel(iOS7)的行数 [英] Calculating number of lines of dynamic UILabel (iOS7)
本文介绍了计算动态UILabel(iOS7)的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这个问题有很多解决方案,但找不到不推荐一个。
There are many solutions to this questions arround but couldn't find non-deprecated one.
我有一个 UILabel
使用模式WordWrap和固定宽度,比方说250.行设置为0.
I have an UILabel
with mode WordWrap and fixed width of, let's say 250. Lines are set to 0.
这是我尝试过的:
UILabel *contentLabel = (UILabel*)[contentView viewWithTag:10];
CGSize size = [contentLabel.text sizeWithFont:contentLabel.font forWidth:contentLabel.frame.size.width lineBreakMode:NSLineBreakByWordWrapping];
NSLog(@"Label's height is: %d", size.height);
高度参数的输出总是20(所以它就像一行),而te文本就像30行。
The output of height param is always 20 (so it's like one line), while te text is like 30 lines long.
我需要用于UIScrollView目的。
I need that for UIScrollView purposes.
推荐答案
使用文档方法中的建议:
Use suggested in documentation method :
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context NS_AVAILABLE_IOS(7_0);
例如
CGSize maxSize = CGSizeMake(self.label.frame.size.width, MAXFLOAT);
CGRect labelRect = [self.label.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.label.font} context:nil];
NSLog(@"size %@", NSStringFromCGSize(labelRect.size));
这篇关于计算动态UILabel(iOS7)的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文