UILabel sizeToFit method not working properly
Solution 1
Turns out the code is just fine - but the Use Autolayout was checked. Unchecked it - everything works just great...
Solution 2
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;
Solution 3
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.
Solution 4
try
textLabel.adjustsFontSizeToFitWidth = YES;
textLabel.minimumFontScale = 0.5;
Solution 5
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.
Related videos on Youtube
YogevSitton
Updated on January 05, 2020Comments
-
YogevSitton over 4 years
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?
-
Piyuesh over 10 yearsis "Lines" property of UILabel is 0 ?
-
YogevSitton over 10 yearsno - "textLabel.numberOfLines = 1;"
-
Piyuesh over 10 yearsTry making it 0.......
-
YogevSitton over 10 yearsChanged it to 0 - now it shows the text in more than one line
-
YogevSitton over 10 yearsIf you read the first sentence of my question you can see I wrote "I'm trying to show a long chunk of text inside a UILabel in one line.". So unfortunately - still unsolved :)
-
Neil Galiaskarov over 10 yearsyour autolayout is checked?
-
YogevSitton over 10 yearsThat was the problem! Thanks!
-
-
YogevSitton over 10 yearsnow it doesn't show the "..." but it doesn't show the whole text. And the UISCrollView does not scroll anymore...
-
HughHughTeotl almost 9 yearsFor me, the trick was to ensure the position of the label was pinned - and then the size also worked itself out as expected. And careful with
adjustsFontSizeToFitWidth
- it will just reduce the font size if the text wouldn't otherwise fit, which may not at all be what you want. -
vikzilla about 8 yearsI noticed this works well also, but am wondering if it is good practice to leave a view with ambiguity
-
Maxthon Chan about 8 years@vikzilla This might be intentional. When the constraints are left in ambiguity some kind of default behavior kicks in, and in this case the label autosizes to fit. You can still leverage this behavior by putting maximum and minimum restraints though.