I have a UILabel measuring 984x728 px with variable text. How can I calculate the maximum font size to fit the UILabel?
Asked
Active
Viewed 3,416 times
1
Rob Napier
- 266,633
- 34
- 431
- 565
scholl
- 81
- 1
- 6
-
1use [lable setAdjustsFontSizeToFitWidth:YES]; – Vijay-Apple-Dev.blogspot.com Aug 30 '11 at 09:29
1 Answers
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
- 266,633
- 34
- 431
- 565
-
2The 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