ios UILabel sizeToFit 方法无法正常工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20042769/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:24:44  来源:igfitidea点击:

UILabel sizeToFit method not working properly

iosobjective-cuiscrollviewuilabelsizetofit

提问by YogevSitton

I'm trying to show a long chunk of text inside a UILabel in one line. The UILabel is a subview of UIScrollView so I can scroll and see the entire UILabel.

我试图在一行中显示 UILabel 内的一大段文本。UILabel 是 UIScrollView 的子视图,因此我可以滚动并查看整个 UILabel。

My problem is that the sizeToFit method only partialy works.

我的问题是 sizeToFit 方法仅部分有效。

textLabel.attributedText = attributedString;
textLabel.numberOfLines = 1;
[textLabel sizeToFit];
textScrollView.contentSize = CGSizeMake(textLabel.frame.size.width, textLabel.frame.size.height);

The UIScrollView content size gets big enough to show the entire UILable, but for a line like:

UIScrollView 内容大小变得足够大以显示整个 UILable,但对于如下一行:

so i'll try to share some of them here every once in a while."

所以我会尝试每隔一段时间在这里分享一些。”

The UILabel shows:

UILabel 显示:

so i'll try to share som...

所以我会尝试分享一些...

What am I doing wrong?

我究竟做错了什么?

回答by YogevSitton

Turns out the code is just fine - but the Use Autolayout was checked. Unchecked it - everything works just great...

原来代码很好 - 但检查了使用自动布局。取消选中它 - 一切都很好......

回答by Joe Barbour

If you want to achieve this with auto layout turned on it's simple. Just make sure you add numberOfLines

如果您想通过打开自动布局来实现这一点,这很简单。只要确保你添加numberOfLines

textLabel.adjustsFontSizeToFitWidth = YES;
textLabel.numberOfLines = 0;

回答by Maxthon Chan

Surprisingly, if you did not put a constraint on the label's width, this would work:

令人惊讶的是,如果您没有对标签的宽度施加限制,这将起作用:

[textLabel.superview layoutSubviews];

I learned this by trial and error.

我通过反复试验了解到这一点。

回答by Shob-Z

try

尝试

textLabel.adjustsFontSizeToFitWidth  = YES;
textLabel.minimumFontScale      =  0.5;  

回答by Gaurav Rastogi

Since you have restricted your Label to show only one line of Text and truncate the rest , it is behaving the same

由于您已将 Label 限制为仅显示一行文本并截断其余部分,因此行为相同

textLabel.attributedText = attributedString;
textLabel.numberOfLines = 0;
[textLabel sizeToFit];
textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textScrollView.contentSize = CGSizeMake(textLabel.frame.size.width, textLabel.frame.size.height);

Hope it will help you

希望它会帮助你

回答by Christopher Swasey

The most common reason for sizeToFitnot working properly is the UILabelnot having any autolayout constraints, for instance if you're implicitly relying on the view position remaining fixed relative to the top left. Adding any constraint at all (leading, top, centerY, anything) will fix it, presumably because it will result in layoutSubviewsbeing called at some point, as suggested in Maxthon Chan's answer.

sizeToFit无法正常工作的最常见原因是UILabel没有任何自动布局约束,例如,如果您隐式地依赖相对于左上角保持固定的视图位置。添加任何约束(前导、顶部、中心Y、任何东西)都会修复它,大概是因为它会导致在layoutSubviews某些时候被调用,如 Maxthon Chan 的回答中所建议的那样。