NSMutableAttributedString 在调用 addAttribute 方法时不能改变文本颜色

Posted

技术标签:

【中文标题】NSMutableAttributedString 在调用 addAttribute 方法时不能改变文本颜色【英文标题】:NSMutableAttributedString can not change the text color when calling addAttribute method 【发布时间】:2014-10-11 02:59:21 【问题描述】:

我想根据第三部分库“TTTAttributedLabel”更改文本颜色,但这没有意义。这是我的代码:

 TTTAttributedLabel *priceInfoLabel.frame = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(self.view.width-12-realSize.width-priceSize.width, 15, realSize.width+priceSize.width, 18)];

priceInfoLabel.centerY = nameView.height/2;
priceInfoLabel.textColor = kRealPriceColor;
[priceInfoLabel setText:[NSString stringWithFormat:@"%@%@",realPriceText,priceText] afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) 
    NSRange priceStringRange = [[mutableAttributedString string] rangeOfString:priceText options:NSCaseInsensitiveSearch];
    [mutableAttributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:nil size:12] range:priceStringRange];
    [mutableAttributedString addAttribute:UITextAttributeTextColor
                                    value:colorWithIntegerValue(195, 195, 195) range:priceStringRange];
    return mutableAttributedString;
];

【问题讨论】:

【参考方案1】:

尝试使用:

[mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName
                                    value:colorWithIntegerValue(195, 195, 195) range:priceStringRange];

【讨论】:

以上是关于NSMutableAttributedString 在调用 addAttribute 方法时不能改变文本颜色的主要内容,如果未能解决你的问题,请参考以下文章

富文本(NSMutableAttributedString)

NSMutableAttributedString的使用

NSMutableAttributedString使用介绍

在 NSMutableAttributedString 上模拟“initWithAttributedString”的 OCMock 失败

NSMutableAttributedString-富文本的使用

NSMutableAttributedString 未正确呈现