2013-02-27 17 views
13

Sto provando a creare un UILabel multilinea con NSMutableAttributedString. Questo funziona bene quando assegno un attributo alla stringa completa in questo modo:Come eseguire il rendering di un UILabel multilinea con NSMutableAttributedString

UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,100)]; 
[contentLabel setLineBreakMode:NSLineBreakByWordWrapping]; 
[contentLabel setNumberOfLines:0]; 
[contentLabel setFont:[UIFont systemFontOfSize:13]; 

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"hello I am a long sentence that should break over multiple lines"]; 
[string addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13] range:NSMakeRange(0, [string length])]; 

contentLabel.attributedText = string; 

Ma io cosa ho bisogno è di applicare una serie di attributi per i diversi sottocampi del NSAttributedString (a certe parole in grassetto).

UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,100)]; 
[contentLabel setLineBreakMode:NSLineBreakByWordWrapping]; 
[contentLabel setNumberOfLines:0]; 
[contentLabel setFont:[UIFont systemFontOfSize:13]; 

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"hello I am a long sentence that should break over multiple lines"]; 
[string addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13] range:NSMakeRange(3, 5)]; 

contentLabel.attributedText = string; 

Quello che sto trovando è che se lo faccio, il testo non viene più visualizzato su più righe nell'etichetta. Viene visualizzato come una singola linea, centrata verticalmente nel riquadro dell'etichetta.

C'è qualcosa che mi manca qui?

+1

Hai reso l'etichetta abbastanza alta per la seconda linea? –

+0

Non riesco a riprodurre questo su 6.1. Prendo atto che il codice è leggermente errato (manca un elemento) in setFont: line e si utilizza "label" anziché "contentLabel" in un unico punto). Sei sicuro che questo sia il codice vero? –

+0

@Kevin Ballard: Sì, l'etichetta è abbastanza alta. – adriaan

risposta

4

Come discusso nei commenti della domanda, il codice presentato nella questione effettivamente funziona correttamente, e che rendono la stringa attribuito come inteso. (Il problema era altrove nel mio codice)

1

Io uso spesso l'UITextView e assegnarle un testo attribuito con un modo simile:

 // creiamo textView descrizione 
    UITextView *description = [[UITextView alloc] init]; 
    description.frame = CGRectMake(20, 453, 500, 190); 

    NSDictionary *attribute1 = @{NSForegroundColorAttributeName: [UIColor blackColor], 
            NSBackgroundColorAttributeName: [UIColor clearColor], 
            NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Bold" size:22.0], 
            }; 

    NSDictionary *attribute2 = @{NSForegroundColorAttributeName: [UIColor blackColor], 
             NSBackgroundColorAttributeName: [UIColor clearColor], 
             NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:10], 
             }; 

    NSDictionary *attribute3 = @{NSForegroundColorAttributeName: [UIColor blackColor], 
              NSBackgroundColorAttributeName: [UIColor clearColor], 
              NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14.0] 
              }; 

    NSMutableAttributedString *info = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ %@ %@ \n\n%@ \n  ", string1, string2 , string3, string4]]; 

    [info setAttributes:attribute1 range:NSMakeRange(0, ([string1 length]))]; 
    [info setAttributes:attribute2 range:NSMakeRange(([string1 length]),([string2 length]+[string3 length]))]; 
    [info setAttributes:attribute3 range:NSMakeRange(([string1 length] [string2 length]+[string3 length]), [string4 length])]; 

    description.attributedText = info; 
    description.backgroundColor = [UIColor clearColor]; 
    description.editable = NO; 
    description.textColor = [UIColor blackColor]; 
    [viewInfo addSubview:description]; 
    [description sizeToFit]; 
Problemi correlati