2013-03-15 18 views
5

Desidero visualizzare i primi 500 caratteri in UILabel e visualizzare l'icona Tronca se sono disponibili più di 500 caratteri.Ma non so come posso limitare 500 caratteri a troncare il testo ?.limitare la UILabel a 500 caratteri e Troncare la UIlabel in IOS

Ecco il mio codice

label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 350, self.bounds.size.width, 30)]; 
    // In this case value of self.bounds.size.width is "427" 

    label2.backgroundColor = [UIColor clearColor]; 
    label2.numberOfLines = 2; 
    label2.textAlignment = UITextAlignmentCenter; 
    label2.font = [UIFont systemFontOfSize:13]; 
    [self addSubview:label2] 

     //Here is Implimentation code of my label 

NSString *temp = [galleryEntryTree objectForKey:@"description"];// calling lebel text from database 
coverView.label2.text = temp; 
coverView.label2.adjustsFontSizeToFitWidth = NO; 
coverView.label2.lineBreakMode = UILineBreakModeTailTruncation; 

Dimmi solo ragazzi come posso mostrare carattere min 500 e poi tronca (se più di 500)

Ogni aiuto è apprezzato

+0

coverView.label2.lineBreakMode = UILineBreakModeWordWrap; e regola la tua altezza o aumenta dinamicamente l'altezza. –

+0

È possibile provare a utilizzare NSMutableString e troncare il testo prima di impostare label.text – jcesarmobile

+0

yourLabel.text.length utilizzare questo in if e quindi utilizzare stringbyappendingstring o stringwithformat – amar

risposta

12

Basta troncare la stringa se è più lunga di 500 caratteri. Solo avvertimento: assicuratevi di non romperlo nel bel mezzo di un surrogate pair:

NSString *temp = [galleryEntryTree objectForKey:@"description"]; 
if ([temp length] > 500) { 
    NSRange range = [temp rangeOfComposedCharacterSequencesForRange:(NSRange){0, 500}]; 
    temp = [temp substringWithRange:range]; 
    temp = [temp stringByAppendingString:@" …"]; 
} 
coverView.label2.text = temp; 
4

Per visualizzare solo 500 caratteri basta usare sotto il codice:

NSString *string = YOUR_TEXT; 
if ([string length] >500) { 
    string = [string substringToIndex:500]; 
} 

Spero che questo ti aiuti .

Tutto il meglio !!!

+2

Non dividere mai le stringhe senza accertarsi di non interrompere le coppie di surrogati. –

0

Può essere semplicemente fatto da

NSString *LabelNewText = [YourLabelName.text substringToIndex:500]; 

Qui 500 o meno di 500 caratteri verrà visualizzato e verrà troncato tutti i personaggi che superano questo.

NSString * string = LabelNewText.text;

if ([string length] >500) 
{ 
    string = [string substringToIndex:500];          
} 
+1

Arresta in modo anomalo se la lunghezza del testo è inferiore a 500. –

+0

È possibile controllare la lunghezza della stringa mediante NSString * string = LabelNewText.text; if ([lunghezza stringa]> 500) { string = [string substringToIndex: 500]; } –

1

prova questo ti aiuta.

label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 350, self.bounds.size.width, 30)]; 
    // In this case value of self.bounds.size.width is "427" 
[email protected]"your text................................"; 
if([label2.text length]>500) 
     label2.text=[label2.text substringToIndex:500]; 


    label2.backgroundColor = [UIColor clearColor]; 
    label2.numberOfLines = 2; 
    label2.textAlignment = UITextAlignmentCenter; 
    label2.font = [UIFont systemFontOfSize:13]; 
    [self addSubview:label2] 
2

Ecco come farlo in Swift 2.2:

let maxLength = 500 
if originalString.characters.count > maxLength { 
    let range = originalString.rangeOfComposedCharacterSequencesForRange(Range<String.Index>(originalString.startIndex ..< originalString.startIndex.advancedBy(maxLength))) 
    let tmpValue = originalString.substringWithRange(range).stringByAppendingString(" …") 
    // use tmpValue 
} 
0

Swift 3.0 versione

let maxLength = 300 //char length 
if originalString.characters.count > maxLength { 
      let range = originalString.rangeOfComposedCharacterSequences(for: originalString.startIndex..<originalString.index(originalString.startIndex, offsetBy: maxLength)) 
      let tmpValue = originalString.substring(with: range).appending("...") 
     }