Revision: 29311
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at July 26, 2010 21:59 by rabc
Initial Code
#import "NSString+TruncateToWidth.h" #define ellipsis @"�¢ï¿½�¦" @implementation NSString (TruncateToWidth) - (NSString*)stringByTruncatingToWidth:(CGFloat)width withFont:(UIFont *)font { // Create a copy that will be truncated and returned NSMutableString *truncatedString = [[self mutableCopy] autorelease]; // Accommodate for ellipsis we'll tack on the end width -= [ellipsis sizeWithFont:font].width; // Get range for last character in string NSRange range = {truncatedString.length - 1, 1}; // Loop, deleting characters until string fits within width while ([truncatedString sizeWithFont:font].width > width) { // Delete character at end [truncatedString deleteCharactersInRange:range]; // Move back another character range.location--; } // Append ellipsis [truncatedString replaceCharactersInRange:range withString:ellipsis]; return truncatedString; } @end
Initial URL
http://iphonedevelopertips.com/cocoa/truncate-an-nsstring-and-append-an-ellipsis-respecting-the-font-size.html
Initial Description
Initial Title
Truncate a String
Initial Tags
iphone
Initial Language
Objective C