2013-02-27 38 views
13

Tôi đang cố gắng tạo một UILabel nhiều dòng với NSMutableAttributedString. này hoạt động tốt khi tôi gán một thuộc tính vào chuỗi hoàn chỉnh như thế này:Cách hiển thị UILabel đa dòng với 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; 

Nhưng tôi những gì tôi cần là để áp dụng một số thuộc tính cho subranges khác nhau của NSAttributedString (lời một số đậm).

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; 

Điều tôi thấy là nếu tôi làm điều này, văn bản không được hiển thị trên nhiều dòng trong nhãn nữa. Nó được hiển thị dưới dạng một dòng, được căn giữa theo chiều dọc trong khung của nhãn.

Có điều gì tôi thiếu ở đây không?

+1

Bạn đã tạo nhãn đủ cao cho dòng thứ hai chưa? –

+0

Tôi không thể sao chép điều này trên 6.1. Tôi lưu ý rằng mã của bạn hơi sai (có một thiếu] trong dòng setFont: và bạn sử dụng "label" thay vì "contentLabel" ở một nơi). Bạn có chắc đây là mã thực tế không? –

+0

@Kevin Ballard: Có, nhãn đủ cao. – adriaan

Trả lời

4

Như đã thảo luận trong ý kiến ​​của câu hỏi, mã thể hiện trong câu hỏi thực sự hoạt động một cách chính xác, và không làm cho chuỗi gán như dự định. (Sự cố ở nơi khác trong mã của tôi)

1

tôi sử dụng thường xuyên các UITextView và gán cho nó một văn bản do một cách tương tự:

 // 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];