pensé que esto era una pregunta interesante, por lo que apenas se construyó y probó esta ...
- (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;
}
Llamada así:
[self setText:self.label withText:@"Now is the time for all good men to come to the aid of their country" andTruncationSuffix:@" more"];
Si esto funciona para usted, usted podría considere agregar una subclase de UILabel, usando esto para anular el método setText: y agregando una propiedad llamada truncatedSuffix.
¿Ha considerado establecer la propiedad de la etiqueta 'ajusteFontSizeToFitWidth' en YES para que la etiqueta ajuste el tamaño de fuente automáticamente? – kevboh
Creo que @thematerik está hablando de comportamiento cuando alcanza el tamaño mínimo de letra y trunca el reemplazo con puntos suspensivos. Esta es una pregunta interesante. – danh