2013-02-27 44 views
13

我想用NSMutableAttributedString創建多行UILabel。 當我給你這樣的完整的字符串的屬性也能正常工作:如何使用NSMutableAttributedString呈現多行UILabel

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; 

但我什麼,我需要的是應用一系列的NSAttributedString的不同子範圍的屬性(大膽某些詞)。

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; 

我在發現的是,如果我這樣做,文本不會再通過標籤中的多行顯示。它被渲染爲一條線,垂直居中在標籤的框架中。

有什麼我在這裏失蹤?

+1

您是否將標籤設計爲足夠高的第二行? – 2013-02-27 17:51:09

+0

我無法在6.1上重現這一點。我注意到你的代碼在setFont:行中有些不正確(缺少],你在一個地方使用「label」而不是「contentLabel」)。你確定這是實際的代碼嗎? – 2013-02-27 18:02:05

+0

@凱文巴拉德:是的,標籤足夠高。 – adriaan 2013-02-27 21:08:58

回答

4

由於在問題的意見討論,題目中的代碼實際工作正常,並沒有呈現屬性串如預期。 (問題在我的代碼中的其他地方)

1

我經常使用的UITextView併爲其分配一個類似的方式歸於文本:

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