2016-05-27 28 views
6

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.

+0

Perché vuoi sapere quanti caratteri sono stati troncati? –

+0

@KingBabar Voglio visualizzarlo all'utente –

+0

È possibile per te utilizzare un UITextView' invece di un 'UILabel'? –

risposta

1

Si sta controllando la larghezza ma è necessario controllare l'altezza.

Dai un'occhiata a questo open source e scopri come TTTAttribuitoLabel utilizza CoreText per sapere quando aggiungere il carattere "...".

https://github.com/TTTAttributedLabel/TTTAttributedLabel/blob/master/TTTAttributedLabel/TTTAttributedLabel.m

sguardo al drawframesetter: attributedstring: TextRange: inrect: contesto: metodo

È anche possibile modificare il open source ed esporre la posizione di '...'

+0

Poiché si controlla solo la larghezza, ignorare la spaziatura della linea che si traduce in un cattivo risultato –

1

Puoi farlo come,

- (void)viewDidLoad { 

    [super viewDidLoad]; 
    UILabel *label1 = self.myLabel; 


UIFont *font = label1.font; 
NSString *text = @"This is the label text wich will be truncate!!"; 

CGRect label1Frame = label1.frame; 

NSUInteger numberOfCharsInLabel1 = NSNotFound; 

NSUInteger numberOfCharactersThatTruncated = NSNotFound; 

for (int i = [text length]; i >= 0; i--) { 
    NSString *substring = [text substringToIndex:i]; 
    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:substring 
                     attributes:@{ NSFontAttributeName : font }]; 
    CGSize size = CGSizeMake(label1Frame.size.width, CGFLOAT_MAX); 
    CGRect textFrame = [attributedText boundingRectWithSize:size 
                options:NSStringDrawingUsesLineFragmentOrigin 
                context:nil]; 

    if (CGRectGetHeight(textFrame) <= CGRectGetHeight(label1Frame)) { 
     numberOfCharsInLabel1 = i; 

     numberOfCharactersThatTruncated = [text length] - numberOfCharsInLabel1; 
     break; 
    } 
} 

if (numberOfCharsInLabel1 == NSNotFound) { 
    // TODO: Handle this case. 
} 

label1.text = [text substringToIndex:numberOfCharsInLabel1]; // If you want to set character that label only can accept without truncating 

//OR 


label1.text = text; //set full text with truncating 


NSLog(@"Number of Truncated Characters %d",numberOfCharactersThatTruncated); 


// Do any additional setup after loading the view. 
} 

Qui self.myLabel fa riferimento presa dell'etichetta.

numberOfCharactersThatTruncated è il tuo desired output.

restituirà il numero di caratteri nell'etichetta che vengono troncati.

Ciò restituirà caratteri totalmente troncati Voglio dire, se la stringa è Hello there ed etichetta è in grado di mostrare hell poi risultato dovrebbe essere 7 ma si vedrà in etichetta qualcosa come h... perché etichetta mostra tre punti quando Tronca. Quindi, se si desidera contare i caratteri che non vengono visualizzati a tutti allora avete bisogno di aggiungere 3 nell'output finale cioè,

numberOfCharactersThatTruncated = numberOfCharactersThatTruncated + 3; 

Aggiornamento:

telaio della mia etichetta è: (27 , 75,93,56)

Ho appena impostato i vincoli top,leading,fixed height,fixed width.

Ans stesso codice funziona come fascino.

Penso che tu stia commettendo errori nel contare i personaggi.Lo spazio bianco dovrebbe essere considerato come un personaggio. Quindi se l'etichetta mostra tre punti significa che l'etichetta di tre caratteri può mostrare.

Esempio: la stringa è: come stai?

personaggio così totale è: 12

ora ad esempio l'etichetta indica: (. Compreso (punti)) come una ...

che significa che mostra 8 caratteri

Quindi, numberOfCharactersThatTruncated tornerà 4.

Se state pensando che how a sono solo i caratteri visibili e non considerando . allora si dovrebbe aggiungere 3-numberOfCharactersThatTruncated.

Se si desidera visualizzare solo i caratteri che possono essere inseriti nell'etichetta, utilizzare label1.text = [text substringToIndex:numberOfCharsInLabel1];.

Non mostrerà il punto.

Cheers .... :)

+0

scusa per risposta in ritardo perché il mio computer ha avuto problemi. Ho controllato la tua soluzione ma non calcolerà correttamente se la mia etichetta ha più righe –

+0

Calcola bene nella mia demo se ci sono più righe !!! – Lion

+0

puoi condividere la tua demo. inoltre creo un progetto demo e utilizzo il tuo codice ma calcola errato –