UILabel sizeToFit method not working properly

YogevSitton picture YogevSitton · Nov 18, 2013 · Viewed 51.6k times · Source

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?

Answer

YogevSitton picture YogevSitton · Nov 18, 2013

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