1

UITextView我使用时显示格式化文本NSAttributedString

NSAttributedString* s1 =
[[NSAttributedString alloc] initWithString:@"Hello there!"
                                attributes:@{NSForegroundColorAttributeName:[UIColor redColor], NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleBody]}];
_textView.attributedText = s1;

但是,当我使用时,NSMutableAttributedString我看到附加的属性字符串没有格式化。格式化将只显示它初始化的字符串。

UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody]
    NSMutableAttributedString *emailBody = [[NSMutableAttributedString alloc] initWithString:@"" attributes:@{NSForegroundColorAttributeName:[UIColor redColor], NSFontAttributeName: font}];
if (_person.firstName.length > 0) {
    [emailBody appendAttributedString:[[NSAttributedString alloc]initWithString:_person.firstName attributes:@{NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleCaption1]}]];
    [emailBody appendAttributedString:[[NSAttributedString alloc]initWithString:@"\n" attributes:nil]];
}
if (_person.lastName.length > 0) {
    [emailBody appendAttributedString:[[NSAttributedString alloc]initWithString:_person.lastName attributes:@{NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleCaption2]}]];
    [emailBody appendAttributedString:[[NSAttributedString alloc]initWithString:@"\n" attributes:nil]];
}

为什么?

4

1 回答 1

1

NSAttributedString附加到 时NSMutableAttributedString,它有自己的属性。

于 2013-10-07T12:40:37.947 回答