在OSX上,我有一个带有深色图像的NSButton,不幸的是,无法使用属性检查器更改颜色。看到图片的黑色大按钮,文本为进入

有任何可能改变文字颜色的线索吗?我查看了NSButton类,但是没有方法可以做到这一点。我知道我可以用白色字体制作图像,但这不是我想要的。

瑞士的问候,罗纳德·霍夫曼
---

最佳答案

这是另外两个解决方案:
http://denis-druz.okis.ru/news.534557.Text-Color-in-NSButton.html

解决方案1:

-(void)awakeFromNib
{
    NSColor *color = [NSColor greenColor];
    NSMutableAttributedString *colorTitle = [[NSMutableAttributedString alloc] initWithAttributedString:[button attributedTitle]];
    NSRange titleRange = NSMakeRange(0, [colorTitle length]);
    [colorTitle addAttribute:NSForegroundColorAttributeName value:color range:titleRange];
    [button setAttributedTitle:colorTitle];
}

解决方案2:

在* .m文件中:
- (void)setButtonTitleFor:(NSButton*)button toString:(NSString*)title withColor:(NSColor*)color
{
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setAlignment:NSCenterTextAlignment];
    NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:color, NSForegroundColorAttributeName, style, NSParagraphStyleAttributeName, nil];
    NSAttributedString *attrString = [[NSAttributedString alloc]initWithString:title attributes:attrsDictionary];
    [button setAttributedTitle:attrString];
}

-(void)awakeFromNib
{
    NSString *title = @"+Add page";
    NSColor *color = [NSColor greenColor];
    [self setButtonTitleFor:button toString:title withColor:color];
}

关于objective-c - NSButton如何给文本着色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13109964/

10-14 06:46