I have a multiline UILabel whose font size I'd like to adjust depending on the text length. The whole text should fit into the label's frame without truncating it.
Unfortunately, according to the documentation the adjustsFontSizeToFitWidth
property "is effective only when the numberOfLines
property is set to 1".
I tried to determine the adjusted font size using
-[NSString (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode]
and then decrementing the font size until it fits. Unfortunately, this method internally truncates the text to fit into the specified size and returns the size of the resulting truncated string.
Best Answer
In this question, 0x90 provides a solution that - although a bit ugly - does what I want. Specifically, it deals correctly with the situation that a single word does not fit the width at the initial font size. I've slightly modified the code so that it works as a category on
NSString
:To use it with a
UILabel
:EDIT: Fixed the code to initialize
newFont
withfont
. Fixes a crash under certain circumstances.