Under the Bridge

Snippet: String Truncation

Here’s a handy NSString category for truncating strings on the iPhone:

Truncate a String and Append an Ellipsis, Respecting the Font Size

Particularly informative if you’ve been either relying on UIControls for your formatting or doing everything with low level CoreGraphics and have overlooked the handy methods in UIStringDrawing.h

// Single line, no wrapping. Truncation based on the UILineBreakMode.

- (CGSize)sizeWithFont:(UIFont *)font; // Uses UILineBreakModeWordWrap

- (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode;

And it looks like you can mix these as well as the string drawing conveniences which follow into your CoreGraphics rendering and everything works together nicely, which does rather simplify things nicely compared to using the raw CoreGraphics text functions.

0