1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-23 16:24:02 +00:00

Optimized stringFittingInWidth

This commit is contained in:
Eric Petit 2006-02-09 13:44:14 +00:00
parent 544cc37263
commit 9545453d48

View file

@ -63,13 +63,50 @@
- (NSString *) stringFittingInWidth: (float) width
withAttributes: (NSDictionary *) attributes
{
NSString * newString = self;
unsigned i;
for (i = [self length]; [newString sizeWithAttributes: attributes].width > width; i--)
newString = [[self substringToIndex: i] stringByAppendingString: NS_ELLIPSIS];
return newString;
float w;
int i;
NSString * newString;
w = [self sizeWithAttributes: attributes].width;
if( w <= width )
/* The whole string fits */
return self;
/* Approximate how many characters we'll need to drop... */
i = [self length] * width / w - 1;
/* ... then refine it */
newString = [[self substringToIndex: i]
stringByAppendingString: NS_ELLIPSIS];
w = [newString sizeWithAttributes: attributes].width;
if( w < width )
{
NSString * bakString;
for( ;; )
{
bakString = newString;
newString = [[self substringToIndex: ++i]
stringByAppendingString: NS_ELLIPSIS];
if( [newString sizeWithAttributes: attributes].width > width )
return bakString;
}
}
else if( w > width )
{
for( ;; )
{
newString = [[self substringToIndex: --i]
stringByAppendingString: NS_ELLIPSIS];
if( [newString sizeWithAttributes: attributes].width <= width )
return newString;
}
}
else
{
return newString;
}
}
@end