UILabel sizeToFit method not working properly

2019-02-11 22:09发布

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.

My problem is that the sizeToFit method only partialy works.

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:

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

The UILabel shows:

so i'll try to share som...

What am I doing wrong?

7条回答
Rolldiameter
2楼-- · 2019-02-11 23:08

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;
查看更多
登录 后发表回答