如何使用NSMutableAttributedString渲染多行UILabel

13
我正在尝试创建一个多行UILabel,使用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;

但我需要做的是为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
你把标签的高度调整得足够容纳第二行了吗? - Lily Ballard
我在6.1版本上无法重现这个问题。我注意到你的代码有一点错误(在setFont:行缺少一个],并且在某个地方你使用了"label"而不是"contentLabel")。你确定这就是实际的代码吗? - Rob Napier
@Kevin Ballard:是的,标签高度足够。 - adriaan
3
@KevinBallard:结果证明你是正确的,我之前计算并分配给标签框架的高度是错误的。感谢你帮我找到正确的方向。 - adriaan
1
你应该把你最后的评论写成一个答案并接受它,这样其他用户(比如我)就能立即看到它。 - thgc
显示剩余2条评论
2个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接