I use a table view to show a list of books, where each cell has a UILabel
that shows the book\'s name and another UILabel
the shows the book\'s aut
Edit: Generalized the solution to work with any "truncation location" string. Previous version only truncated at instance of string @" +"
. Edit allows you to define where you want the truncation to happen.
I took my answer from this question (which was an answer modified from the answer on this site) and tailored it to fit your needs. Create a new NSString
interface where you can send your string to be custom-truncated.
NOTE: This solution is for iOS 7+ only. To use in iOS 6, use sizeWithFont:
instead of sizeWithAttributes:
in the NSString+TruncateToWidth.m file.
NSString+TruncateToWidth.h
@interface NSString (TruncateToWidth)
- (NSString*)stringByTruncatingAtString:(NSString *)string toWidth:(CGFloat)width withFont:(UIFont *)font;
@end
NSString+TruncateToWidth.m
#import "NSString+TruncateToWidth.h"
#define ellipsis @"…"
@implementation NSString (TruncateToWidth)
- (NSString*)stringByTruncatingAtString:(NSString *)string toWidth:(CGFloat)width withFont:(UIFont *)font
{
// If the string is already short enough, or
// if the 'truncation location' string doesn't exist
// go ahead and pass the string back unmodified.
if ([self sizeWithAttributes:@{NSFontAttributeName:font}].width < width ||
[self rangeOfString:string].location == NSNotFound)
return self;
// Create copy that will be the returned result
NSMutableString *truncatedString = [self mutableCopy];
// Accommodate for ellipsis we'll tack on the beginning
width -= [ellipsis sizeWithAttributes:@{NSFontAttributeName:font}].width;
// Get range of the passed string. Note that this only works to the first instance found,
// so if there are multiple, you need to modify your solution
NSRange range = [truncatedString rangeOfString:string];
range.length = 1;
while([truncatedString sizeWithAttributes:@{NSFontAttributeName:font}].width > width
&& range.location > 0)
{
range.location -= 1;
[truncatedString deleteCharactersInRange:range];
}
// Append ellipsis
range.length = 0;
[truncatedString replaceCharactersInRange:range withString:ellipsis];
return truncatedString;
}
@end
Using it:
// Make sure to import the header file where you want to use it
myLabel.text = [@"Benjamin Walker Jackson + 2 authors" stringByTruncatingAtString:@" +" toWidth:myLabel.frame.size.width withFont:myLabel.font];
// Sample Result: Benjamin Walte... + 2 authors