/ Published in: Objective C
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#import "NSString+TruncateToWidth.h" #define ellipsis @"�¢ï¿½�¦" { // Create a copy that will be truncated and returned // 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