UILabel sizeToFit method not working properly
Asked Answered
C

6

24

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?

Calculus answered 18/11, 2013 at 8:1 Comment(7)
is "Lines" property of UILabel is 0 ?Wichman
no - "textLabel.numberOfLines = 1;"Calculus
Try making it 0.......Wichman
Changed it to 0 - now it shows the text in more than one lineCalculus
If 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 :)Calculus
your autolayout is checked?Inessive
That was the problem! Thanks!Calculus
C
29

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

Calculus answered 18/11, 2013 at 8:25 Comment(0)
P
23

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;
Planchette answered 15/7, 2014 at 6:55 Comment(1)
For 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.Rad
R
4

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.

Retake answered 30/3, 2015 at 5:29 Comment(2)
I noticed this works well also, but am wondering if it is good practice to leave a view with ambiguityTrevino
@Trevino 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.Retake
W
2

try

textLabel.adjustsFontSizeToFitWidth  = YES;
textLabel.minimumFontScale      =  0.5;  
Winnifredwinning answered 18/11, 2013 at 8:24 Comment(0)
W
1

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

Winer answered 18/11, 2013 at 8:17 Comment(1)
now it doesn't show the "..." but it doesn't show the whole text. And the UISCrollView does not scroll anymore...Calculus
T
1

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.

Tasty answered 18/7, 2016 at 17:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.