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
- (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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With