首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSSegmentedControl (子类NSSegmentCell)文本颜色

NSSegmentedControl (子类NSSegmentCell)文本颜色
EN

Stack Overflow用户
提问于 2013-10-14 11:03:24
回答 1查看 1.1K关注 0票数 0

我创建一个NSSegmentedControl作为一个源代码的多个徽章。第一段具有绿色,并显示匹配不同规则的项目数。第二段有一个红色和计数无与伦比的规则。NSSegmentedControl是禁用的,因此用户无法单击它。文本颜色是灰色的,因为它是禁用的。

怎样才能改变文字的颜色?我尝试用方法"setAttributedStringValue:“在NSSegmentCell子类中设置颜色,但它不起作用。

代码语言:javascript
复制
[self setAttributedStringValue:string];
EN

回答 1

Stack Overflow用户

发布于 2014-05-26 01:47:37

我不确定这个信息对你是否有帮助,但如果你还在寻找答案,…

要设置属性字符串的文本颜色,必须使用键NSForegroundColorAttributeName向属性字符串的属性字典中添加一个NSForegroundColorAttributeName对象。我会教你一些我知道怎么做的方法。

NSString 对象:创建属性化字符串的

代码语言:javascript
复制
NSDictionary *attrs = @{NSForegroundColorAttributeName:[NSColor redColor]};
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:@"Hello"
                                                              attributes:attrs];

来自现有NSAttributedString 对象的:

代码语言:javascript
复制
NSMutableAttributedString *tempAttributedString = anExistingAttributedString.mutableCopy;
[tempAttributedString addAttribute:NSForegroundColorAttributeName 
                             value:[NSColor redColor] 
                             range:NSMakeRange(0, tempAttributedString.length)];
anExistingAttributedString = tempAttributedString;

因此,在子类中,您可能希望截获传递给-setAttributedStringValue:的值,如下所示:

代码语言:javascript
复制
- (void)setAttributedStringValue:(NSAttributedString *)attributedStringValue
{
    NSMutableAttributedString *temp = attributedStringValue.mutableCopy;
    [temp addAttribute:NSForegroundColorAttributeName 
                 value:[NSColor redColor] 
                 range:NSMakeRange(0, temp.length)];
    [super setAttributedStringValue:temp];
}

这可能是最好的处理方法,也可能不是最好的方式,但由于子类方面的信息太少,对我来说这似乎是个不错的选择。如果你已经知道了这一切,我很抱歉你的回答太长了。如果你不这么做,希望这能帮上忙!祝好运。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19358778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档