Live From Denver

Double Encore, Inc.'s blog is place for us to share our thoughts about iPhone development, technology, entrepreneurship, or anything else that strikes our fancy. We encourage you to join the dialogue.



iPhone Dev Tip: Resizing UILabel Text With numberOfLines & adjustsFontSizeToWidth
Posted by Dan Burcaw on January 18th, 2009

UILabel has a fantastic property called adjustsFontSizeToWidth which comes in handy if you want your text to be dynamically resized to fit the label’s bounding frame. In particular this feature makes a lot of sense if your UILabel can only occupy a finite amount of space within your view.

You can use minimumFontSize to constrain how small the label will shrink your font. If you constrain the font size and your text doesn’t fit, standard line break rules will apply such as Tail Truncation or Word Wrap.

One caveat: there appears to be a bug in UILabel that prevents the resizing from working if numberOfLines is set to a value other than 1.

UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 60)];

// adjustsFontSizeToFitWidth only works if numberOfLines is set to 1
titleLabel.numberOfLines = 1;
titleLabel.adjustsFontSizetoWidth = YES;
titleLabel.minimumFontSize = 8.0f;

If you want a multiline UILabel, you might have to give up some screen real estate and resize the label’s bounding frame horizontally depending upon the length of text.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • del.icio.us
  • Facebook
  • Google
  • Technorati
  • TwitThis
>> back to the blog







©2009 Double Encore, Inc. All Rights Reserved.