简体   繁体   English

更改 NSButton 的背景颜色

[英]Change background color of NSButton

I have an NSButton in a mac application whose colour I'd like to change programatically but nothing I tried seems to work.我在 mac 应用程序中有一个 NSButton,我想以编程方式更改其颜色,但我尝试的任何方法似乎都不起作用。 I tried to create an output on the NSButtonCell and set the background color there but that didn't work either.我试图在 NSButtonCell 上创建一个 output 并在那里设置背景颜色,但这也不起作用。 Any code snippets would be helpful.任何代码片段都会有所帮助。

Assuming everything is hooked up in IB for your borderless button.假设一切都在 IB 中为您的无边框按钮连接。

// *.h file
IBOutlet NSButton* myButton;

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

Note from the setBackgroundColor documentation: setBackgroundColor文档中的注意事项:

"The background color is used only when drawing borderless buttons." “背景颜色仅在绘制无边框按钮时使用。”

If this won't do it for you then you'll need to override NSButton and implement the drawing yourself.如果这对您不起作用,那么您需要覆盖 NSButton 并自己实现绘图。

Good Luck.祝你好运。

Here is another possible way to do this:这是另一种可能的方法:

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

The standard Aqua (pill-shaped) buttons are drawn by the system.标准的 Aqua(药丸状)按钮由系统绘制。 They don't have a background color as such.他们没有这样的背景颜色。 In fact, they are composed of images that Cocoa stitches together to make a coherent button image.事实上,它们由 Cocoa 拼接在一起的图像组成,以形成一个连贯的按钮图像。 So Cocoa can't recolor the images to your liking.所以 Cocoa 无法根据您的喜好重新着色图像。 Only the default button (the one with Return set as its key equivalent) will have a blue pulsing background.只有默认按钮(将 Return 设置为其等效键的按钮)将具有蓝色脉冲背景。

What it sounds like you want to do will involve subclassing NSButtonCell and doing your own drawing.听起来您想要做的将涉及子类化NSButtonCell并进行自己的绘图。 If you wanted to recolor the button images to get the effect you want, you can use the excellent Theme Park utility to extract copies of Apple's button images and use those.如果您想重新着色按钮图像以获得您想要的效果,您可以使用出色的Theme Park实用程序来提取 Apple 按钮图像的副本并使用它们。 I'll admit to having done this myself in order to "steal" certain interface elements that aren't otherwise accessible, such as the scrollers from iTunes.我承认自己这样做是为了“窃取”某些其他方式无法访问的界面元素,例如 iTunes 中的滚动条。

If anyone face problem for storyboard setup then for reference check: You can set properties directly to NSButton as shown below如果有人在故事板设置方面遇到问题,请进行参考检查:您可以将属性直接设置为 NSButton,如下所示

: 在此处输入图片说明

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

I've created a NSButton subclass called FlatButton that makes it super-easy to change button background color, text color, icon color, borders and other types of styling, from Interface Builder:我创建了一个名为FlatButtonNSButton子类,它可以非常轻松地从 Interface Builder 更改按钮背景颜色、文本颜色、图标颜色、边框和其他类型的样式:

https://github.com/OskarGroth/FlatButton https://github.com/OskarGroth/FlatButton

适用于 macOS 的 FlatButton

Mark's answer is correct.马克的回答是正确的。 But if doing it by code will be messy for you, then another way to do it is to set it directly in interface builder.但是如果通过代码来做对你来说会很麻烦,那么另一种方法是直接在界面构建器中设置它。

  1. Find the button in interfaceBuilder and click on it.在 interfaceBuilder 中找到按钮并单击它。
  2. On the object browser section of interface builder you will see the button that you clicked on has a dropdown disclosure icon.在界面构建器的对象浏览器部分,您将看到您单击的按钮有一个下拉显示图标。 Click on it.点击它。 It will then show you the NSButtonCell of the NSButton.然后它会向您显示 NSButton 的 NSButtonCell。 Click on it.点击它。
  3. With the NSButtonCell selected, Click on the Identity-Inspector.选择 NSButtonCell 后,单击 Identity-Inspector。
  4. In the Identity-inspector look for the section called User Defined Runtime Attributes在 Identity-inspector 中查找名为User Defined Runtime Attributes
  5. Click on the little plus (+) sign to add a value.单击小加号 (+) 以添加值。 The Value will show to have 3 properties.该值将显示具有 3 个属性。 (A) keyPath (B) Type (C) Value (A) keyPath (B)类型(C)
  6. Change the keyPath to: backgroundColor .将 keyPath 更改为: backgroundColor Change the type to: Color .将类型更改为: Color Change the value to: what ever color you want (a color picker will be displayed)将值更改为:您想要的任何颜色(将显示颜色选择器)

Done.完毕。

One method is to create a view behind the button and set the colour of that.一种方法是在按钮后面创建一个视图并设置它的颜色。 This creates the view with the same frame and the puts the button on top of the view.这将创建具有相同框架的视图,并将按钮放在视图的顶部。 I use a tag on the button to make sure we only add the view once, so the method can be called repeatedly when drawing the UI.我在按钮上使用了一个标签来确保我们只添加一次视图,所以在绘制 UI 时可以重复调用该方法。

I've included my code for setting the text colour.我已经包含了用于设置文本颜色的代码。

Change the colour and alpha as desired:根据需要更改颜色和 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];
}

In Swift you can do like this:在 Swift 中,你可以这样做:

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

Thanks for these great answers.感谢这些伟大的答案。 I've compiled an NSButton subclass that makes it easy to set background color, border attributes (color, width, corners), and title attributes (color, font).我编译了一个 NSButton 子类,它可以轻松设置背景颜色、边框属性(颜色、宽度、角)和标题属性(颜色、字体)。

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

If anybody is still looking at this.如果有人还在看这个。 This code works in Swift 5...此代码适用于 Swift 5 ...

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

... you have to set needsLayer or add a layer in the nib first. ...你必须先设置 NeedsLayer 或在笔尖中添加一个图层。 It works with borders as well.它也适用于边界。

When using macOS 11 or newer, you can use the bezelColor property when you have a button with bezelStyle set to .rounded or regularSquare in code, or when the button style is set to 'Push' or 'Bezel' in the interface designer of Xcode.使用 macOS 11 或更高版本时,当您在代码bezelStyle设置为.roundedregularSquare的按钮,或者在 Xcode 的界面设计器中将按钮样式设置为“Push”或“Bezel”时,可以使用bezelColor属性.

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

you can choose your own color by clicking "Other..." option from color paletteUse "#colorLiteral" to select the color from the GUI color palette.您可以通过单击调色板中的“其他...”选项来选择自己的颜色使用“#colorLiteral”从 GUI 调色板中选择颜色。

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

看图片

UIElement(Element is of anything Like Button,view....) UIElement(元素是任何像按钮,视图....)

UIElement *varname; UIElement *varname;

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

eg: UIButton *pButton;例如: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