scholl
scholl

Reputation: 81

How can I calculate font size for my UILabel?

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

Upvotes: 1

Views: 3428

Answers (1)

Rob Napier
Rob Napier

Reputation: 299345

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.

Upvotes: 9

Related Questions