Automatically adjusting UILabel fontsize

13,848

Just use the adjustsFontSizeToFitWidth property of UILabel [Reference].

la.adjustsFontSizeToFitWidth = YES;

From the reference:

Normally, the label text is drawn with the font you specify in the font property. If this property is set to YES, however, and the text in the text property exceeds the label’s bounding rectangle, the receiver starts reducing the font size until the string fits or the minimum font size is reached. This property is effective only when the numberOfLines property is set to 1.

The default value for this property is NO. If you change it to YES, you should also set an appropriate minimum font size by modifying the minimumFontSize property.

Share:
13,848
Illep
Author by

Illep

I love writing code.

Updated on June 04, 2022

Comments

  • Illep
    Illep almost 2 years

    I have a label of length 100. This is the maximum label size i can have.

    When i display a text of 10 characters it fits to it. But at times i will have to display more than 10 characters. So is there a way to shrink the the font size and make it to fit in my UILabel of length 100 ?

    UILabel *la = [UILabel alloc]initWithFrame(5,10,100,14)];