繁体   English   中英

更改 NSButton 的背景颜色

[英]Change background color of NSButton

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

假设一切都在 IB 中为您的无边框按钮连接。

// *.h file
IBOutlet NSButton* myButton;

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

setBackgroundColor文档中的注意事项:

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

如果这对您不起作用,那么您需要覆盖 NSButton 并自己实现绘图。

祝你好运。

这是另一种可能的方法:

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

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

听起来您想要做的将涉及子类化NSButtonCell并进行自己的绘图。 如果您想重新着色按钮图像以获得您想要的效果,您可以使用出色的Theme Park实用程序来提取 Apple 按钮图像的副本并使用它们。 我承认自己这样做是为了“窃取”某些其他方式无法访问的界面元素,例如 iTunes 中的滚动条。

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

在此处输入图片说明

结果 :
在此处输入图片说明

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

https://github.com/OskarGroth/FlatButton

适用于 macOS 的 FlatButton

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

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

完毕。

一种方法是在按钮后面创建一个视图并设置它的颜色。 这将创建具有相同框架的视图,并将按钮放在视图的顶部。 我在按钮上使用了一个标签来确保我们只添加一次视图,所以在绘制 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];
}

在 Swift 中,你可以这样做:

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

感谢这些伟大的答案。 我编译了一个 NSButton 子类,它可以轻松设置背景颜色、边框属性(颜色、宽度、角)和标题属性(颜色、字体)。

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

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

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

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

使用 macOS 11 或更高版本时,当您在代码bezelStyle设置为.roundedregularSquare的按钮,或者在 Xcode 的界面设计器中将按钮样式设置为“Push”或“Bezel”时,可以使用bezelColor属性.

let button = NSButton()
button.title = "Example"
button.bezelStyle = .rounded // or .regularSquare
button.bezelColor = .controlAccentColor

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

Button.layer?.backgroundColor = #colorLiteral(red: 0, green: 0.4797514677, blue: 0.9984372258, alpha: 1)

看图片

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]];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM