1

I have a UILabel measuring 984x728 px with variable text. How can I calculate the maximum font size to fit the UILabel?

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
scholl
  • 81
  • 1
  • 6

1 Answers1

9

Create a font larger than you would ever want (48 points is probably good). Then use this:

CGFloat maxFontSize;
[string sizeWithFont:font minFontSize:0 actualFontSize:&maxFontSize forWidth:maxWidth lineBreakMode: UILineBreakModeClip];

maxFontSize will hold the largest size less than 48 and greater than 0 that will fit within maxWidth.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • 2
    The API Rob mentions `sizeWithFont:` has been deprecated in iOS7. Please see this Stack Overflow question for a more complete response: http://stackoverflow.com/questions/18897896/replacement-for-deprecated-sizewithfont-in-ios-7 – Jeremy C. Feb 26 '14 at 22:37