好吧,长话短说:
NSButton的字体NSButton子类中,我想给它们着色,就像Xcode选项卡项的颜色一样。

我是这样设置颜色的(作为一个简单的NSColor):
NSColor *color = [NSColor colorWithCalibratedRed:0.09 green:0.55 blue:0.90 alpha:1.0];
NSMutableAttributedString *colorTitle =
[[NSMutableAttributedString alloc] initWithAttributedString:[self attributedTitle]];
NSRange titleRange = NSMakeRange(0, [colorTitle length]);
[colorTitle addAttribute:NSForegroundColorAttributeName
value:color
range:titleRange];
[self setAttributedTitle:colorTitle];如何将其设置为NSGradient
发布于 2014-10-14 10:37:06
好吧,这是解决办法,对于任何可能发现有用的人来说.
步骤1:
在NSColor上创建一个基于@Omz的great answer的类别。在下面的代码中,您将看到它被重命名为colorFromGradient:,只是为了与通常的Cocoa命名约定很好地结合在一起.
步骤2:
用渐变颜色重绘标题
NSColor* gS = [NSColor colorWithCalibratedRed:0.07 green:0.47 blue:0.87 alpha:1.0];
NSColor* gE = [NSColor colorWithCalibratedRed:0.12 green:0.64 blue:0.94 alpha:1.0];
NSGradient* g = [[NSGradient alloc] initWithStartingColor:gE endingColor:gS];
NSColor *color = [NSColor colorFromGradient:g];
NSMutableAttributedString *colorTitle =
[[NSMutableAttributedString alloc] initWithAttributedString:[self attributedTitle]];
NSRange titleRange = NSMakeRange(0, [colorTitle length]);
[colorTitle addAttribute:NSForegroundColorAttributeName
value:color
range:titleRange];
[self setAttributedTitle:colorTitle];步骤3:
享受结果吧。:-)

https://stackoverflow.com/questions/26358368
复制相似问题