/* Truncate a string to a desired width, appending ellipses... Original Source: (See copyright notice at ) */ /*" Truncate a string, returning substring with "É" to fit in the given width. Probably not the most efficient way of doing this; perhaps an optimization would be to decide on the font of the "É" and compensate for that in the width calculations; another would be to do a binary search on the length, zeroing in on the optimum length. "*/ - (NSAttributedString *)truncateForWidth:(int) inWidth { NSAttributedString *result = self; if ([self size].width > inWidth) { NSMutableAttributedString *newString = [[[NSMutableAttributedString alloc] init] autorelease]; int curLength = [self length] - 1; // start by chopping off at least one [newString appendAttributedString:self]; while ([newString size].width > inWidth) { NSRange range = NSMakeRange( curLength - 1, 2); // replace 2 characters with "É" [newString replaceCharactersInRange:range withString:@"É"]; curLength--; } result = newString; } return result; }