Dynamically changing font size

Dynamically changing font size – UILabel

up vote38down votefavorite


I currently have a UILabel:

    factLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 280, 100)];
    factLabel.text = @"some text some text some text some text";
    factLabel.backgroundColor = [UIColor clearColor];
    factLabel.lineBreakMode = UILineBreakModeWordWrap;
    factLabel.numberOfLines = 10;
    [self.view addSubview:factLabel];

Throughout the life of my iOS application, factLabel gets a bunch of different values. Some with multiple sentences. Others with just 5 or 6 words.

How can I set up the UILabel so that the font size changes so that the text always fits in the bounds I defined?

iphone ios fonts uilabel
share|improve this question
asked Feb 1 ’11 at 16:53
  add comment

6 Answers

up vote88down voteaccepted
factLabel.numberOfLines = 1;
factLabel.minimumFontSize = 8.;
factLabel.adjustsFontSizeToFitWidth = YES;

The above code will adjust your text’s font size down to (for example) 8 trying to fit your text within the label. numberOfLines = 1 is mandatory.

For numberOfLines > 1 there is a method to figure out the size of final text through NSString’s UIKit addition methods, for example:

CGSize lLabelSIze = [yourText sizeWithFont: factLabel.font forWidth:factLabel.frame.size.width lineBreakMode:factLabel.lineBreakMode];

After that you can just resize your label using resulting lLabelSIze, for example (assuming that you will change only label’s height):

factLabel.frame = CGRectMake(factLabel.frame.origin.x, factLabel.frame.origin.y, factLabel.frame.size.width, lLabelSIze.height);


Starting with iOS6, minimumFontSize has been deprecated. The line

factLabel.minimumFontSize = 8.;

can be changed to:

factLabel.minimumScaleFactor = 8./factLabel.font.labelFontSize;
share|improve this answer
edited Sep 27 ’13 at 7:19
answered Feb 1 ’11 at 17:03
Martin Babacaev
but this puts the text all on one line. and if I change the factLabel.numberOfLines, then the font size does not change dynamically. –  CodeGuy Feb 1 ’11 at 17:06
@reising1: you’re right. This is just how to make framework to do resizing work for you. –  Martin BabacaevFeb 1 ’11 at 17:10
so then the answer to my question is that there is no way to do it using the provided framework? – CodeGuy Feb 1 ’11 at 17:12
@reising1: In this case you also can use NSString UIKit addition’s method: sizeWithFont:constrainedToSize:lineBreakMode: But this way is a little bit difficult – Martin Babacaev Feb 1 ’11 at 17:31 
It’s deprecated since iOS6. Replace it with myLabel.minimumScaleFactor:10.0/[UIFont labelFontSize]; –  Norbert Sep 21 ’13 at 17:11

show 5 more comments

up vote15down vote

minimumFontSize has been deprecated with iOS6. You can use minimumScaleFactor.


this will take care of your font size according width of label & text.

Please let me know if any concerns.

share|improve this answer
edited Sep 17 ’13 at 7:40
answered Feb 19 ’13 at 10:47
Amit Singh
  add comment
up vote4down vote

Its a little bit not sophisticated but this should work, for example lets say you want to cap your uilabel to 120×120, with max font size of 28:

magicLabel.numberOfLines = 0;
magicLabel.lineBreakMode = NSLineBreakByWordWrapping;
magicLabel.text = text;
    for (int i = 28; i>3; i--) {
        CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:(CGFloat)i] constrainedToSize:CGSizeMake(120.0f, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
        if (size.height < 120) {
            magicLabel.font = [UIFont systemFontOfSize:(CGFloat)i];
share|improve this answer
edited Jun 11 ’13 at 22:51
answered Jun 11 ’13 at 22:41
Eyal Ben Dov
  add comment
up vote2down vote
lable.font = [UIFont fontWithName:@"Arial" size:15];
share|improve this answer
edited Aug 13 ’12 at 8:00
answered Jun 22 ’12 at 5:59
This is a much better answer. Thanks! –  Johno Jun 28 ’12 at 16:02
thank you for the simplicity. upvoted! –  Pavan Aug 13 ’12 at 16:23 
If you want to avoid hard coding the system font, you can do [UIFont systemFontOfSize:15] –  PetterAug 14 ’12 at 21:46 
This is not an answer to the asked question which is “Dynamically changing font size”. –  NobleK Sep 25 ’12 at 6:24

add comment

up vote1down vote

Just send the sizeToFit message to the UITextView. It will adjust its own height to just fit its text. It will not change its own width or origin.

[textViewA1 sizeToFit];
share|improve this answer
edited Mar 5 at 11:10
answered Nov 20 ’12 at 4:13
  add comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s