我正在尝试在一行中向UILABEL中的一大块文字展示。 Uilabel是UIScrollView的子视图,所以我可以滚动并查看整个UILabel。

我的问题是Sizetofit方法只能占用。

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

UiscrollView内容大小足以显示整个UILABLE,但是对于如此:

所以我会尝试每次在这里分享一些它们。“

Uilabel显示:

所以我会尝试分享som ...

我做错了什么?

有帮助吗?

解决方案

拒绝代码很精细 - 但是检查了使用autolayout。 取消选中它 - 一切都很棒...

其他提示

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

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

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.

try

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

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

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

The most common reason for sizeToFit not working properly is the UILabel not 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 layoutSubviews being called at some point, as suggested in Maxthon Chan's answer.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top