ho pensato che questa era una domanda interessante, così costruito e appena testato questo ...
- (void)setText:(UILabel *)label withText:(NSString *)text andTruncationSuffix:(NSString *)truncationSuffix {
// just set the text if it fits using the minimum font
//
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:label.minimumFontSize]];
if (size.width <= label.bounds.size.width) {
label.text = text;
return;
}
// build a truncated version of the text (using the custom truncation text)
// and shrink the truncated text until it fits
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;
}
chiamare in questo modo:
[self setText:self.label withText:@"Now is the time for all good men to come to the aid of their country" andTruncationSuffix:@" more"];
Se questo funziona per voi, si potrebbe prendere in considerazione l'aggiunta di una sottoclasse di UILabel, usando questo per sovrascrivere il metodo setText: e aggiungendo una proprietà chiamata truncatedSuffix.
Avete considerato l'impostazione 'proprietà adjustsFontSizeToFitWidth' dell'etichetta su YES in modo da avere l'etichetta di regolare automaticamente la dimensione del carattere? – kevboh
Penso che @thematerik stia parlando di comportamento quando raggiunge la dimensione minima del font e poi tronca la sostituzione con ellissi. Questa è una domanda interessante. – danh