Calculating Height of Multi-Line Text on the iPhone SDK 07/07/2009
If you've done some text layout on iPhone, you'll know that the documentation isn't really the greatest and clearest. The most frequently annoying thing about text layout is that there are these NSString UIStringDrawing additions that you would think can help you determine the size required to render a piece of text with a given font.
Specifically:
- (CGSize)sizeWithFont:(UIFont *)font
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode
- (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode
Only one of the four above gives you sizes with multi-line text. Worse still, even if you choose the right one, you probably will end up passing the wrong thing into the size parameter.
In order to get the height of a piece of multi line text constrained by a given width, you must do this:
CGSize boundingSize = CGSizeMake(desiredWidth, CGFLOAT_MAX);
CGSize requiredSize = [someText sizeWithFont:someFont
constrainedToSize:boundingSize
lineBreakMode:UILineBreakModeWordWrap];
CGFloat requiredHeight = requiredSize.height;
Things to note:
- You must pass in
CGFLOAT_MAX(or a big number) for the height, otherwise your text will never flow outside of theboundingSize. This is the bit I always get wrong. - All the other
sizeWithFontmethods do not work with multi-line text. It says so in the documentation, even though it is not easy to spot. - You must use
UILineBreakModeWordWrapfor thelineBreakMode
- 1 comment
- 8 months, 1 week ago (07/07/2009)

Add a comment
Atom Feed for the Blog Entries