首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将NSGradient转化为NSColor

将NSGradient转化为NSColor
EN

Stack Overflow用户
提问于 2014-10-14 10:20:08
回答 1查看 730关注 0票数 0

好吧,长话短说:

  • 我使用(嵌入到包中的) FontAwesome
  • 我正在使用它作为一些自定义NSButton的字体
  • NSButton子类中,我想给它们着色,就像Xcode选项卡项的颜色一样。

我是这样设置颜色的(作为一个简单的NSColor):

代码语言:javascript
复制
    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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-14 10:37:06

好吧,这是解决办法,对于任何可能发现有用的人来说.

步骤1:

NSColor上创建一个基于@Omz的great answer的类别。在下面的代码中,您将看到它被重命名为colorFromGradient:,只是为了与通常的Cocoa命名约定很好地结合在一起.

步骤2:

用渐变颜色重绘标题

代码语言:javascript
复制
    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:

享受结果吧。:-)

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

https://stackoverflow.com/questions/26358368

复制
相关文章

相似问题

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