我创建一个NSSegmentedControl作为一个源代码的多个徽章。第一段具有绿色,并显示匹配不同规则的项目数。第二段有一个红色和计数无与伦比的规则。NSSegmentedControl是禁用的,因此用户无法单击它。文本颜色是灰色的,因为它是禁用的。
怎样才能改变文字的颜色?我尝试用方法"setAttributedStringValue:“在NSSegmentCell子类中设置颜色,但它不起作用。
[self setAttributedStringValue:string];发布于 2014-05-26 01:47:37
我不确定这个信息对你是否有帮助,但如果你还在寻找答案,…
要设置属性字符串的文本颜色,必须使用键NSForegroundColorAttributeName向属性字符串的属性字典中添加一个NSForegroundColorAttributeName对象。我会教你一些我知道怎么做的方法。
从NSString 对象:创建属性化字符串的
NSDictionary *attrs = @{NSForegroundColorAttributeName:[NSColor redColor]};
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:@"Hello"
attributes:attrs];来自现有NSAttributedString 对象的:
NSMutableAttributedString *tempAttributedString = anExistingAttributedString.mutableCopy;
[tempAttributedString addAttribute:NSForegroundColorAttributeName
value:[NSColor redColor]
range:NSMakeRange(0, tempAttributedString.length)];
anExistingAttributedString = tempAttributedString;因此,在子类中,您可能希望截获传递给-setAttributedStringValue:的值,如下所示:
- (void)setAttributedStringValue:(NSAttributedString *)attributedStringValue
{
NSMutableAttributedString *temp = attributedStringValue.mutableCopy;
[temp addAttribute:NSForegroundColorAttributeName
value:[NSColor redColor]
range:NSMakeRange(0, temp.length)];
[super setAttributedStringValue:temp];
}这可能是最好的处理方法,也可能不是最好的方式,但由于子类方面的信息太少,对我来说这似乎是个不错的选择。如果你已经知道了这一切,我很抱歉你的回答太长了。如果你不这么做,希望这能帮上忙!祝好运。
https://stackoverflow.com/questions/19358778
复制相似问题