ios 计算动态 UILabel 的行数(iOS7)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18784183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculating number of lines of dynamic UILabel (iOS7)
提问by Jacek Kwiecień
There are many solutions to this questions arround but couldn't find non-deprecatedone.
这个问题周围有很多解决方案,但找不到非弃用的解决方案。
I have an UILabel
with mode WordWrap and fixed width of, let's say 250. Lines are set to 0.
我有一个UILabel
带模式的 WordWrap 和固定宽度,比方说 250。行设置为 0。
Here is what I tried:
这是我尝试过的:
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);
The output of height param is always 20 (so it's like one line), while te text is like 30 lines long.
height 参数的输出总是 20(所以它就像一行),而 te 文本就像 30 行长。
I need that for UIScrollView purposes.
我需要它用于 UIScrollView 目的。
回答by Numeral
Use suggested in documentation method :
在文档方法中建议使用:
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context NS_AVAILABLE_IOS(7_0);
E.g.
例如
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));
回答by AlexD
I was having trouble using boundingRectWithSize directly on my UILabel's attributedText — it was notaccounting for the wrap to multiple lines, (the returned height was always 17.5). To work around this, I had to use boundingRectWithSize on the UILabel's textproperty and pass in the attributes dictionary separately (and notvia [self.myLabel.attributedText attributesAtIndex:0 effectiveRange:nil]
).
我在我的 UILabel 的属性文本上直接使用 boundingRectWithSize 时遇到了问题——它没有考虑到多行的换行(返回的高度总是 17.5)。为了解决这个问题,我必须在 UILabel 的text属性上使用 boundingRectWithSize并单独传入属性字典(而不是通过[self.myLabel.attributedText attributesAtIndex:0 effectiveRange:nil]
)。
CGRect labelSize = CGRectIntegral([self.myLabel.text
boundingRectWithSize:CGSizeMake(self.myLabel.frame.size.width, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
attributes:@{NSFontAttributeName:self.myLabel.font,
NSParagraphStyleAttributeName:paragraphStyle} context:nil]);
回答by Sarat Patel
You can use this simple method:
您可以使用这个简单的方法:
which will return number of lines
这将返回行数
- (int)lineCountForLabel:(UILabel *)label
{
CGFloat labelWidth = label.frame.size.width;
int lineCount = 0;
CGSize textSize = CGSizeMake(labelWidth, MAXFLOAT);
long rHeight = lroundf([label sizeThatFits:textSize].height);
long charSize = lroundf(label.font.leading);
lineCount = (int)( rHeight / charSize );
return lineCount;
}
by calling
通过调用
[self lineCountForLabel:YOUR_LABEL];
回答by onkar dhanlobhe
let l = UILabel()
l.numberOfLines = 0
l.layer.frame.size.width = self.view.frame.width - 40 /*(Padding 20 + 20)*/
l.font = UIFont(name: "BwModelica-Bold", size: 16.0)
l.text = "Your Any length Text!!"
let noOfLines = ceil(l.intrinsicContentSize.width) / l.frame.width
let lbl_height = noOfLines * l.intrinsicContentSize.height
This will be your Exact dynamic height of Label. Happy coding!!!
这将是您 Label 的确切动态高度。快乐编码!!!
回答by Siddh
Try this out
试试这个
//Here tweetText is an object of NSString and assign a text to it
NSString *tweetText = tweet.tweetText;
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [tweetText sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
//detailLabel is an object of UILabel
[detailLabel setText:tweetText];
//Set frame
[detailLabel setFrame:CGRectMake(76,20,280, MAX(size.height, 30.0f))];
回答by ninja_iOS
Here is method which I'm using:
这是我正在使用的方法:
CGSize maximumSize = CGSizeMake(contentLabel.frame.size.width, 9999);
CGSize myStringSize = [eventName sizeWithFont:contentLabel.font
constrainedToSize:maximumSize
lineBreakMode:UILineBreakModeWordWrap];