【问题标题】:NSGradient into NSColorNSGradient 变成 NSColor
【发布时间】:2014-10-14 10:20:08
【问题描述】:

好的,长话短说:

  • 我正在使用(嵌入到捆绑包中)FontAwesome
  • 我在一些自定义NSButtons 中使用它作为字体
  • 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

【问题讨论】:

    标签: objective-c xcode cocoa nscolor nsgradient


    【解决方案1】:

    好的,这就是解决方案,适合任何可能觉得有用的人...

    第 1 步:

    基于@Omz 的great answerNSColor 上创建一个类别。在下面的代码中,您将看到它重命名为 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 步:

    享受结果。 :-)

    【讨论】:

    • colorFromGradient: 似乎不再是有效的 API。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-20
    • 2011-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多