2013-02-27 9 views
13

Próbuję utworzyć wielowierszową UILabel z NSMutableAttributedString. Działa to dobrze, kiedy przypisać atrybut do kompletnego łańcucha takiego:Jak renderować multilinię UILabel z 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; 

Ale co potrzebne jest, aby zastosować szereg atrybutów dla poszczególnych podzakresów z NSAttributedString (pogrubienie niektórych słów).

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; 

To, co znajduję, to to, że jeśli to zrobię, tekst nie będzie już renderowany przez wiele linii w etykiecie. Jest renderowany jako pojedyncza linia, wyśrodkowany pionowo w ramce etykiety.

Czy jest tu coś, czego mi brakuje?

+1

Czy etykietka była wystarczająco wysoka dla drugiej linii? –

+0

Nie mogę tego odtworzyć w wersji 6.1. Zauważam, że twój kod jest trochę niepoprawny (brakuje go) w linii setFont: i zamiast tego używasz "etykiety" zamiast "contentLabel" w jednym miejscu). Jesteś pewien, że to jest rzeczywisty kod? –

+0

@Kevin Ballard: Tak, etykieta jest wystarczająco wysoka. – adriaan

Odpowiedz

4

Jak wspomniano w komentarzach na pytanie, w kod przedstawiony w pytaniu faktycznie działa poprawnie i nie czynią nadany ciąg jak zamierzono. (Problem był w innym miejscu w moim kodzie).

1

używam często UITextView i przypisać mu tekst nadany w podobny sposób:

 // 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]; 
Powiązane problemy