Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to truncate an NSString based on the graphical width?

In UILabel there's functionality to truncate labels using different truncation techniques (UILineBreakMode). In NSString UIKit Additions there is a similar functionality for drawing strings.

However, I found no way to access the actual truncated string. Is there any other way to get a truncated string based on the (graphical) width for a given font?

I'd like to have a category on NSString with this method:

-(NSString*)stringByTruncatingStringWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode
like image 764
Ortwin Gentz Avatar asked Feb 15 '10 14:02

Ortwin Gentz


2 Answers

- (NSString*)stringByTruncatingStringWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode {
    NSMutableString *resultString = [[self mutableCopy] autorelease];
    NSRange range = {resultString.length-1, 1};

    while ([resultString boundingRectWithSize:CGSizeMake(FLT_MAX, FLT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil].size.width > width) {
        // delete the last character
        [resultString deleteCharactersInRange:range];
        range.location--;
        // replace the last but one character with an ellipsis
        [resultString replaceCharactersInRange:range withString:truncateReplacementString];
    }
    return resultString;
}

Note that since iOS 6 this method is not safe to run on background threads anymore.

like image 104
Ortwin Gentz Avatar answered Nov 04 '22 19:11

Ortwin Gentz


One option is trying different sizes by looping until you get the right width. I.e. start with the full string, if that's wider than what you need, replace the last two characters with an ellipsis character. Loop until it's narrow enough.

If you think you'll be working with long strings, you can binary search your way towards the truncation point to make it a bit faster.

like image 35
uliwitness Avatar answered Nov 04 '22 17:11

uliwitness