, , ...
- (void)setText:(UILabel *)label withText:(NSString *)text andTruncationSuffix:(NSString *)truncationSuffix {
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:label.minimumFontSize]];
if (size.width <= label.bounds.size.width) {
label.text = text;
return;
}
NSInteger lastIndex = text.length;
CGFloat width = MAXFLOAT;
NSString *subtext, *ellipticalText;
while (lastIndex > 0 && width > label.bounds.size.width) {
subtext = [text substringToIndex:lastIndex];
ellipticalText = [subtext stringByAppendingString:truncationSuffix];
width = [ellipticalText sizeWithFont:[UIFont systemFontOfSize:label.minimumFontSize]].width;
lastIndex--;
}
label.text = ellipticalText;
}
:
[self setText:self.label withText:@"Now is the time for all good men to come to the aid of their country" andTruncationSuffix:@" more"];
, UILabel, , setText: , truncatedSuffix.