UILabel sizeToFit method not working properly

52,681

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.

Share:
52,681

Related videos on Youtube

YogevSitton
Author by

YogevSitton

Updated on January 05, 2020

Comments

  • YogevSitton
    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
      Piyuesh over 10 years
      is "Lines" property of UILabel is 0 ?
    • YogevSitton
      YogevSitton over 10 years
      no - "textLabel.numberOfLines = 1;"
    • Piyuesh
      Piyuesh over 10 years
      Try making it 0.......
    • YogevSitton
      YogevSitton over 10 years
      Changed it to 0 - now it shows the text in more than one line
    • YogevSitton
      YogevSitton over 10 years
      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 :)
    • Neil Galiaskarov
      Neil Galiaskarov over 10 years
      your autolayout is checked?
    • YogevSitton
      YogevSitton over 10 years
      That was the problem! Thanks!
  • YogevSitton
    YogevSitton over 10 years
    now it doesn't show the "..." but it doesn't show the whole text. And the UISCrollView does not scroll anymore...
  • HughHughTeotl
    HughHughTeotl almost 9 years
    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.
  • vikzilla
    vikzilla about 8 years
    I noticed this works well also, but am wondering if it is good practice to leave a view with ambiguity
  • Maxthon Chan
    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.