2013-05-08 15 views
5

Ho una stringa come,Come visualizzare diversi colori e tipi di font in un UILabel

NSSting *[email protected]"First Second Third Fourth Fifth Sixth Seventh"; 

Voglio visualizzare questa stringa nella singola etichetta con diversi tipi di font e colori.

cioè ..,

stringa "In primo luogo" dovrebbe essere in grassetto.

La stringa "Second" deve essere in corsivo.

La "terza" stringa deve essere in colore rosso.

La "quarta" stringa deve essere di colore verde.

La "quinta" stringa deve essere in grassetto corsivo del carattere di sistema.

La "sesta" stringa deve essere in grassetto.

"Settimo" stringa dovrebbe essere Arial grassetto di taglia 34.

Ho passato con alcune delle risposte a StackOverflow, ma non ho avuto alcuna chiarezza quelle risposte ..

Can qualsiasi uno fornisce alcune linee guida.

Grazie in anticipo.

Soluzione: manipolare innanzitutto NSAttributedString in base alle proprie esigenze.

utilizzando NSAttributedString si può fare in iOS 6 UILabel ha attributedString property.

Below ios 6.0 utilizzare TTAtributeLabel o qualsiasi third party attributedLabel per displaying attributedString.

+0

Non puoi farlo in un'unica etichetta. 7 etichette diverse che ti servono –

+1

usando NSAttributedString puoi farlo in ios6 uilabel ha attribuito proprietà Proprietà. –

+0

Sotto ios 6.0 utilizzare TTAtributeLabel per visualizzare attributeString. –

risposta

1

in iOS 6.0 e versioni successive UILabel ha una proprietà:
@property(nonatomic,copy) NSAttributedString *attributedText
che accetta un NSAttributedString.

È possibile creare uno NSAttributedString corrispondente ai propri requisiti.

+0

cosa per meno di iOS 6.0 ???? –

0

Provatelo ....

DAAttributedStringUtils

e anche vedere this

Different Label

Speranza ho aiutato

+0

controlla questo http://soulwithmobiletechnology.blogspot.in/2012/07/how-to-use-nsattributedstring-in-ios-6.html –

+0

@Prince: cosa dici? per favore spiegami in dettaglio –

3

utilizzare questo codice per impostare il colore diverso per parola diversa.

NSString *test = @"First Second Third Fourth Fifth Sixth Seventh"; 

CFStringRef string = (CFStringRef) test; 
    CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0); 
    CFAttributedStringReplaceString (attrString,CFRangeMake(0, 0), string); 



    /* 
    Note: we could have created CFAttributedStringRef which is non mutable, then we would have to give all its 
    attributes right when we create it. We can change them if we use mutable form of CFAttributeString. 
    */ 



    //Lets choose the colors we want to have in our string 
    CGColorRef _orange=[UIColor orangeColor].CGColor; 
    CGColorRef _green=[UIColor greenColor].CGColor; 
    CGColorRef _red=[UIColor redColor].CGColor; 
    CGColorRef _blue=[UIColor blueColor].CGColor;  




    //Lets have our string with first 20 letters as orange 
    //next 20 letters as green 
    //next 20 as red 
    //last remaining as blue 
    CFAttributedStringSetAttribute(attrString, CFRangeMake(0, 20),kCTForegroundColorAttributeName, _orange); 
    CFAttributedStringSetAttribute(attrString, CFRangeMake(20, 20),kCTForegroundColorAttributeName, _green); 
    CFAttributedStringSetAttribute(attrString, CFRangeMake(40, 20),kCTForegroundColorAttributeName, _red);  
    CFAttributedStringSetAttribute(attrString, CFRangeMake(60, _stringLength-61),kCTForegroundColorAttributeName, _blue); 

Inoltre è possibile impostare Colore con Immagine come muggito ..

[yourLable setTextColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImageName"]]]; 

See My Un'altra risposta Da This Link

Spero che questo sia utile a voi ...

1

se u non usa NSAttributeLabel quindi prova questo, funziona perfettamente per me

aggiungere questo metodo nella classe CommonFuction & aggiungere questo quadro CoreText.framework

+ (void)setMultiColorAndFontText:(NSString *)text rangeString:(NSArray *)rangeString label:(UILabel*) label font:(NSArray*) fontName color:(NSArray*) colorName 
{ 
    label.layer.sublayers = nil; 

    NSMutableAttributedString *mutableAttributedString = [[ NSMutableAttributedString alloc]initWithString:text]; 

    for (int i =0 ; i<[rangeString count]; i++) 
    { 
     CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef) [UIFont fontWithName:[fontName objectAtIndex:i] size:10.0].fontName, [UIFont fontWithName:[fontName objectAtIndex:i] size:10.0].pointSize, NULL); 

     NSRange whiteRange = [text rangeOfString:[rangeString objectAtIndex:i]]; 

     if (whiteRange.location != NSNotFound) 
     { 
      [mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[colorName objectAtIndex:i] range:whiteRange]; 
      [mutableAttributedString addAttribute:(NSString*)kCTFontAttributeName 
              value:(__bridge id)ctFont 
              range:whiteRange]; 
     } 
    } 

    CGSize expectedLabelSize = [text sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:10.0f] constrainedToSize:CGSizeMake(186,100) lineBreakMode:UILineBreakModeWordWrap]; 

    CATextLayer *textLayer = [[CATextLayer alloc]init]; 
    textLayer.frame =CGRectMake(0,0, label.frame.size.width,expectedLabelSize.height+4); 
    textLayer.contentsScale = [[UIScreen mainScreen] scale]; 
    textLayer.string=mutableAttributedString; 
    textLayer.opacity = 1.0; 
    textLayer.alignmentMode = kCAAlignmentLeft; 
    [label.layer addSublayer:textLayer]; 
    [textLayer setWrapped:TRUE]; 

    //label.lineBreakMode = UILineBreakModeWordWrap; 

    label.text = @""; 
} 

e dopo chiamata di questo metodo come segue: -

[CommonFunctions setMultiColorAndFontText:string rangeString:[NSArray arrayWithObjects:str1,str2,str3,str4,str5,str6, nil] label:yourLabel font:[NSArray arrayWithObjects:@"Arial",@"Arial",@"Arial",@"Arial",@"Arial",@"Arial"nil] color:[NSArray arrayWithObjects:(UIColor *)[UIColor colorWithRed:(0/255.f) green:(167/255.f) blue:(230/255.f) alpha:1.0f].CGColor,(UIColor *)[UIColor colorWithRed:(189/255.f) green:(189/255.f) blue:(189/255.f) alpha:1.0f].CGColor, color3,color4,color5,color6,nil]]; 

spero che funziona bene per u

`

0

Ho affrontato lo stesso problema durante lo sviluppo .come NSAttributeString funziona sopra ioS6 n sopra. Invece di ottenere classi di terze parti, ti suggerisco di creare UILabel personalizzato che supporta la personalizzazione dell'etichetta In breve, crea molte etichette (parole per parole) assegnando colori, grassetto, lunghezza. E aggiungi tutte le etichette insieme e crea un nuovo oggetto Label

Problemi correlati