#define elipses @"..." - (NSString*)stringByTruncatingToWidth:(CGFloat)width withFont:(UIFont *)font{ NSMutableString *truncatedString = [[self mutableCopy] autorelease]; if ([self sizeWithFont:font].width > width) { width -= [ellipsis sizeWithFont:font].width; NSRange range = {truncatedString.length - 1, 1}; while ([truncatedString sizeWithFont:font].width > width) { [truncatedString deleteCharactersInRange:range]; range.location--; } [truncatedString replaceCharactersInRange:range withString:ellipsis]; } return truncatedString; }
Pretty darn useful anywhere you're overriding drawRect and don't feel like wrecking your designer's font settings.