Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically adjusting UILabel fontsize

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)];
like image 778
Illep Avatar asked Feb 18 '12 05:02

Illep


1 Answers

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.

like image 83
Peter Sarnowski Avatar answered Oct 26 '22 11:10

Peter Sarnowski