2012-05-01 16 views
5

Ho un UILabel nel mio progetto iPhone che ha una larghezza e altezza fissa ma il suo contenuto può variare a seconda di l'utente sta guardando. A volte il testo è troppo grande per UILabel e cioè quando la stringa: '...' viene aggiunta alla fine della riga. Mi chiedo se potrei cambiare questa stringa con qualcos'altro, ad esempio: "(altro)".Modifica il '...' predefinito alla fine di un testo se il contenuto di un UILabel non si adatta

Grazie!

+0

Avete considerato l'impostazione 'proprietà adjustsFontSizeToFitWidth' dell'etichetta su YES in modo da avere l'etichetta di regolare automaticamente la dimensione del carattere? – kevboh

+0

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

risposta

1

Purtroppo, sembra che una tale opzione non è incluso su iOS, come da questa domanda simile: How to change truncate characters in UILabel?

Tuttavia, come le risposte del citato stato di domanda, questo può essere facilmente da sé fatto. Tutto quello che devi veramente fare è trovare dove viene troncata la stringa e sottrarre la quantità necessaria per il carattere finale scelto. Quindi inserisci il resto in una stringa separata.

Per questo metodo, questa risposta sarebbe anche utile:

Come Javanator detto che avrebbe dovuto fare la propria troncamento. Dovresti usare sizeWithFont: forWidth: lineBreakMode: messaggio sulle aggiunte di UIKit alla classe NSString per ottenere la larghezza di una stringa con un determinato font. Questo gestirà tutti i tipi di carattere.

1

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.

+0

Nel momento in cui il testo è stato impostato, la larghezza dell'etichetta potrebbe non essere ancora determinata, in particolare se si sta utilizzando l'Autolayout. – ozgur

+0

@ozgur - sì, questo post è piuttosto obsoleto ora, dato il layout automatico e tutte le nuove cose tipografiche. anche 'sizeWithFont' è deprecato ora. forse lo riscriverò presto ad un certo punto. – danh

1

Se si utilizza la versione iOS> 8.0, è possibile utilizzare ResponsiveLabel. Qui puoi fornire token di troncamento personalizzati e definire azioni per renderlo utilizzabile.

NSString *expansionToken = @"Read More ..."; 
NSString *str = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."; 
NSMutableAttributedString *attribString = [[NSMutableAttributedString alloc]initWithString:kExpansionToken attributes:@{NSForegroundColorAttributeName:[UIColor blueColor],NSFontAttributeName:self.customLabel.font}]; 
[self.customLabel setAttributedTruncationToken:attribString withAction:^(NSString *tappedString) { 
NSLog(@"Tap on truncation text"); 
}]; 
[self.customLabel setText:str withTruncation:YES]; 
Problemi correlati