NSButton如何给文本上色 [英] NSButton how to color the text
问题描述
在OSX上,我有一个带有深色图像的NSButton,不幸的是,无法使用属性检查器更改颜色.看到图片上的黑色大按钮,文字为 Go .
on OSX I have an NSButton with a pretty dark image and unfortunately it is not possible to change the color using the attributes inspector. See picture the big black button, the text is Go.
是否有任何可能更改文本颜色的线索?我查看了NSButton类,但是没有方法可以做到这一点.我知道我可以用白色字体制作图像,但这不是我想要的.
Any clues for a possibility to change the text color? I looked in to the NSButton class but there is no method to do that. I´m aware that I could make the image with white font but that is not what I want to do.
来自瑞士的问候,罗纳德·霍夫曼(Ronald Hofmann) ---
Greetings from Switzerland, Ronald Hofmann ---
推荐答案
以下是另外两个解决方案:
http://denis-druz.okis. ru/news.534557.Text-Color-in-NSButton.html
Here is two other solutions:
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];
}
这篇关于NSButton如何给文本上色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!