更改NSButton的背景颜色

时间:2009-06-19 11:28:38

标签: objective-c cocoa macos user-interface

我在mac应用程序中有一个NSButton,其颜色我想以编程方式进行更改,但我尝试的任何内容似乎都无法正常工作。我试图在NSButtonCell上创建一个输出并在那里设置背景颜色,但这也不起作用。任何代码片段都会有所帮助。

13 个答案:

答案 0 :(得分:36)

假设IB中的所有内容都与您的无边框按钮挂钩。

// *.h file
IBOutlet NSButton* myButton;

// *.m file
[[myButton cell] setBackgroundColor:[NSColor redColor]];

请注意setBackgroundColor文档:

  

“仅在绘制无边框按钮时使用背景颜色。”

如果这不适合你,那么你需要覆盖NSButton并自己实现绘图。

祝你好运。

答案 1 :(得分:10)

标准的Aqua(药丸形)按钮由系统绘制。它们没有背景颜色。实际上,它们由Cocoa拼接在一起形成连贯按钮图像的图像组成。因此Cocoa无法根据您的喜好重新着色图像。只有默认按钮(将Return设置为其等效键的按钮)将具有蓝色脉冲背景。

你想要做的事情将涉及继承NSButtonCell并做自己的绘图。如果您想重新着色按钮图像以获得所需效果,可以使用优秀的Theme Park实用程序来提取Apple按钮图像的副本并使用它们。我承认自己已经完成了这项工作,以“窃取”某些无法访问的界面元素,例如iTunes中的滚动条。

答案 2 :(得分:10)

这是另一种可能的方法:

    let button = NSButton()
    button.title = ""
    button.bezelStyle = .texturedSquare
    button.isBordered = false //Important
    button.wantsLayer = true
    button.layer?.backgroundColor = NSColor.red.cgColor

答案 3 :(得分:5)

我创建了一个名为NSButton的{​​{1}}子类,它可以非常轻松地从Interface Builder更改按钮背景颜色,文本颜色,图标颜色,边框和其他类型的样式:< / p>

https://github.com/OskarGroth/FlatButton

FlatButton for macOS

答案 4 :(得分:3)

马克的回答是正确的。但是如果通过代码执行它会让你感到麻烦,那么另一种方法是直接在界面构建器中设置它。

  1. 在界面建设器中找到按钮,然后单击它。
  2. 在界面构建器的对象浏览器部分,您将看到您单击的按钮具有下拉列表图标。点击它。然后它会显示NSButton的NSButtonCell。点击它。
  3. 选择NSButtonCell后,单击Identity-Inspector。
  4. 在Identity-inspector中查找名为User Defined Runtime Attributes
  5. 的部分
  6. 点击小加号(+)以添加值。该值将显示有3个属性。 (A) keyPath (B)输入(C)
  7. 将keyPath更改为:backgroundColor。将类型更改为:Color。将值更改为:您想要的颜色(将显示颜色选择器)
  8. 完成。

答案 5 :(得分:1)

一种方法是在按钮后面创建一个视图并设置它的颜色。这将创建具有相同框架的视图,并将按钮放在视图的顶部。我在按钮上使用了一个标签,以确保我们只添加一次视图,因此在绘制UI时可以重复调用该方法。

我已经包含了设置文字颜色的代码。

根据需要更改颜色和alpha:

NSColor *colour = [NSColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.5] ;
[self styleButton:button colour:colour] ;

-(void) styleButton:(NSButton *)button colour:(NSColor *)colour {

    [self setButton:button fontColor:[NSColor whiteColor]];

    if(button.tag == 9901) return ;

    button.bezelStyle = NSBezelStyleTexturedSquare ;
    [button setBordered:NO];

    NSView *b2 = [[NSView alloc] initWithFrame:button.frame] ;
    [button.superview addSubview:b2] ;

    b2.layer.backgroundColor = colour.CGColor;

    [button removeFromSuperview];
    [b2.superview addSubview:button];

    button.tag = 9901 ;
}

-(void) setButton:(NSButton *)button fontColor:(NSColor *)color {
    NSMutableAttributedString *colorTitle = [[NSMutableAttributedString alloc] initWithAttributedString:[button attributedTitle]];
    [colorTitle addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, button.attributedTitle.length)];
    [button setAttributedTitle:colorTitle];
}

答案 6 :(得分:1)

迅速4。 先前的答案对我有所帮助。谢谢。

extension NSButton {

    public func setText(text: String, color: NSColor, font: NSFont) {

        let paragraphStyle = NSMutableParagraphStyle.init()
        paragraphStyle.alignment = .center
        let range = NSRange.init(location: 0, length: (text.lengthOfBytes(using: String.Encoding.utf8)))
        let attributedTitle = NSMutableAttributedString.init(string: text)
        attributedTitle.addAttribute( NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: range)
        attributedTitle.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
        attributedTitle.addAttribute(NSAttributedString.Key.font, value: font, range: range)
        self.attributedTitle = attributedTitle
    }

    public func setBackground(color: NSColor, radius: CGFloat = 2.0) {
        self.wantsLayer = true
        self.isBordered = false
        self.layer?.backgroundColor = color.cgColor
        self.layer?.cornerRadius = radius
    }

}

如何使用IBOutlet设置NSButton:

refreshButton.setText(text: "REFRESH", color: .white, font: .systemFont(ofSize: 13))
refreshButton.setBackground(color: .red, radius: 2.0)

答案 7 :(得分:1)

如果有人遇到故事板设置问题,请参考:您可以将属性直接设置为NSButton,如下所示

enter image description here

结果:
  enter image description here

答案 8 :(得分:0)

感谢这些伟大的答案。 我已经编译了一个NSButton子类,可以很容易地设置背景颜色,边框属性(颜色,宽度,角落)和标题属性(颜色,字体)。

随意使用: https://github.com/philfung/osx-dudley/blob/master/DudNSButton.swift

答案 9 :(得分:0)

在Swift中,您可以这样做:

(button.cell as! NSButtonCell).isBordered = false
(button.cell as! NSButtonCell).backgroundColor = .white

答案 10 :(得分:0)

如果有人还在看这个。此代码适用于 Swift 5...

button.layer?.backgroundColor = NSColor.green.cgColor

...你必须先设置needsLayer或在笔尖中添加一个图层。它也适用于边框。

答案 11 :(得分:-1)

您可以通过单击调色板中的“其他...”选项来选择自己的颜色使用“ #colorLiteral”从GUI调色板中选择颜色。

Button.layer?.backgroundColor = #colorLiteral(红色:0,绿色:0.4797514677,蓝色:0.9984372258,alpha:1)

see image

答案 12 :(得分:-10)

UIElement(元素是按钮,视图....)

UIElement * varname;

[varname setBackgroundColor:[UIColor colorWithRed:0.8 green:0.8 blue:0.6 alpha:1.0]];

例如: UIButton * pButton;

[pButton setBackgroundColor:[UIColor colorWithRed:0.8 green:0.8 blue:0.6 alpha:1.0]];
相关问题