Ho uno UILabel
e voglio determinare quanti caratteri in UILabel
hanno troncato.
Con questo codice qui sotto, posso determinare se il mio UILabel
hanno solo 1 lineaDetermina il numero di caratteri troncati in UILabel
int numberOfTruncatedCharacter = 0;
NSString *text = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s";
NSArray *words = [text componentsSeparatedByString:@" "];
NSString *newStr = @"";
for (NSString *word in words) {
NSString *statement = [NSString stringWithFormat:@"%@ %@",newStr, word];
CGSize size = [statement sizeWithAttributes:@{NSFontAttributeName: self.aboutLabel.font}];
if(size.width < self.aboutLabel.bounds.size.width){
newStr = [NSString stringWithFormat:@"%@ %@",newStr, word];
}else{
numberOfTruncatedCharacter++;
}
NSLog(@"%@ | %f | %f",word,size.width,self.aboutLabel.bounds.size.width);
}
NSLog(@"number of truncated character = %d",numberOfTruncatedCharacter);
Funziona correttamente e qui è il registro
2016-05-27 10:02:44.642 EarCrush[2284:42690] Lorem | 32.622070 | 355.000000
2016-05-27 10:02:44.642 EarCrush[2284:42690] Ipsum | 64.345703 | 355.000000
2016-05-27 10:02:44.642 EarCrush[2284:42690] is | 74.243164 | 355.000000
2016-05-27 10:02:44.643 EarCrush[2284:42690] simply | 107.138672 | 355.000000
2016-05-27 10:02:44.643 EarCrush[2284:42690] dummy | 145.644531 | 355.000000
2016-05-27 10:02:44.643 EarCrush[2284:42690] text | 165.952148 | 355.000000
2016-05-27 10:02:44.644 EarCrush[2284:42690] of | 177.978516 | 355.000000
2016-05-27 10:02:44.644 EarCrush[2284:42690] the | 195.854492 | 355.000000
2016-05-27 10:02:44.645 EarCrush[2284:42690] printing | 235.004883 | 355.000000
2016-05-27 10:02:44.646 EarCrush[2284:42690] and | 255.429688 | 355.000000
2016-05-27 10:02:44.647 EarCrush[2284:42690] typesetting | 309.921875 | 355.000000
2016-05-27 10:02:44.648 EarCrush[2284:42690] industry. | 353.134766 | 355.000000
2016-05-27 10:02:44.649 EarCrush[2284:42690] Lorem | 385.756836 | 355.000000
2016-05-27 10:02:44.649 EarCrush[2284:42690] Ipsum | 384.858398 | 355.000000
2016-05-27 10:02:44.650 EarCrush[2284:42690] has | 372.202148 | 355.000000
2016-05-27 10:02:44.650 EarCrush[2284:42690] been | 379.218750 | 355.000000
2016-05-27 10:02:44.650 EarCrush[2284:42690] the | 371.010742 | 355.000000
2016-05-27 10:02:44.651 EarCrush[2284:42690] industry's | 401.171875 | 355.000000
2016-05-27 10:02:44.651 EarCrush[2284:42690] standard | 397.431641 | 355.000000
2016-05-27 10:02:44.652 EarCrush[2284:42690] dummy | 391.640625 | 355.000000
2016-05-27 10:02:44.652 EarCrush[2284:42690] text | 373.442383 | 355.000000
2016-05-27 10:02:44.653 EarCrush[2284:42690] ever | 375.844727 | 355.000000
2016-05-27 10:02:44.653 EarCrush[2284:42690] since | 379.541016 | 355.000000
2016-05-27 10:02:44.653 EarCrush[2284:42690] the | 371.010742 | 355.000000
2016-05-27 10:02:44.654 EarCrush[2284:42690] 1500s | 383.374023 | 355.000000
2016-05-27 10:02:44.654 EarCrush[2284:42690] number of truncated character = 13
Il problema è quando il mio UILabel
dispone di più linee, a cambiare il mio codice per
if(size.width < self.aboutLabel.bounds.size.width * numberOfLines){
Tuttavia non calcolerà correttamente. Penso che quando UILabel
abbia più righe, conterrà il carattere di interruzione
Qualsiasi idea di risolverlo. Qualsiasi aiuto o suggerimento sarebbe molto apprezzato.
Perché vuoi sapere quanti caratteri sono stati troncati? –
@KingBabar Voglio visualizzarlo all'utente –
È possibile per te utilizzare un UITextView' invece di un 'UILabel'? –