根据内容调整 UILabel 的大小
我有一个 UILabel,他的文本大小具有
title.adjustsFontSizeToFitWidth = YES;
阻止我使用标准方法调整 UILabel 大小的属性。我在这里读到另一篇文章,我应该使用
sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode
这个答案中的函数: 当 -adjustsFontSizeToFitWidth 设置为 YES 时,如何计算出 UILabel 的字体大小?
现在,我不能弄清楚如何让它工作..这是实际的代码
UIFont *font = [UIFont fontWithName:@"Marker Felt" size:200];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, width, 20.0)];
title.text = @"this is a long title that should be resized";
title.font = font;
title.numberOfLines = 1;
title.adjustsFontSizeToFitWidth = YES;
CGFloat pointSize = 0.0;
CGSize size = [title.text sizeWithFont:font
minFontSize:title.minimumFontSize
actualFontSize:&pointSize
forWidth:width
lineBreakMode:title.lineBreakMode];
title.frame = CGRectMake(title.frame.origin.x,
title.frame.origin.y,
size.width,
size.height);
UILabel 错误地调整了大小,就好像字体大小仍然是 200.. 有什么线索吗?谢谢!
I have a UILabel, his text size has the property
title.adjustsFontSizeToFitWidth = YES;
that prevents me from using standard methods to resize the UILabel. I read on another post here that I'm supposed to use the function
sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode
from this answer: How to figure out the font size of a UILabel when -adjustsFontSizeToFitWidth is set to YES?
Now, i can't figure out how to make it work.. this is the actual code
UIFont *font = [UIFont fontWithName:@"Marker Felt" size:200];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, width, 20.0)];
title.text = @"this is a long title that should be resized";
title.font = font;
title.numberOfLines = 1;
title.adjustsFontSizeToFitWidth = YES;
CGFloat pointSize = 0.0;
CGSize size = [title.text sizeWithFont:font
minFontSize:title.minimumFontSize
actualFontSize:&pointSize
forWidth:width
lineBreakMode:title.lineBreakMode];
title.frame = CGRectMake(title.frame.origin.x,
title.frame.origin.y,
size.width,
size.height);
The UILabel get resized wrongly, as if the font size it's still 200..
Any clues? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我有一些代码你可以在我的github上使用,检查一下,它是 UILabel 的一个类别,你需要设置框架宽度,当你可以在 UILabel 上 resizeToFit 时,它会调整高度以适应内容,并返回 y标签末尾的位置,以便您可以调整其后出现的任何内容。
https://gist.github.com/1005520
I have some code you could use on my github, check it out, it's a category for UILabel, you need to set the frame width and when you can resizeToFit on the UILabel, it adjusts the height to fit the content, and returns the y position of the end of the label so you can adjust any content appearing after it.
https://gist.github.com/1005520
我建议将此作为错误提交。
-sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:
返回的大小具有正确的宽度,但高度不考虑实际字体大小。UILabel
似乎也有这个错误。更改标签的大小以匹配-sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:
返回的字体中的文本高度将错误地垂直定位标签内的文本。解决方法是计算正确的高度,并将标签上的字体更改为实际字体大小:
I'd suggest filing this as a bug.
The size returned by
-sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:
has the correct width, but not the height does not account for the actual font size.It seems likely that
UILabel
also has this bug. Changing the size of a label to match the height of the text in a font of the size returned by-sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:
will incorrectly vertically position the text within the label.A work-around is to calculate the correct height, and change the font on the label to with the actual font size:
尝试创建较小字体的字体。例如:
但是要 actualFontSize 将链接传递给 CGFloat == 200。
更新:
然后尝试以下操作:
Try to create font with smaller fontSize. For example:
but to actualFontSize pass link to CGFloat == 200.
UPDATED:
try this then:
我认为你应该使用这段代码,我正在使用滚动视图上的标签来显示大文本,你也可以这样做
I think you should use this code and i am using the label on scrollview for a big text you can also do so
您尝试过intrinsicContentSize吗?
Have you tried intrinsicContentSize?
这段代码对我有用
this code worked for me