objective-c 更改 NSButton 的背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1017468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change background color of NSButton
提问by aleemb
I have an NSButton in a mac application whose colour I'd like to change programatically but nothing I tried seems to work. I tried to create an output on the NSButtonCell and set the background color there but that didn't work either. Any code snippets would be helpful.
我在 Mac 应用程序中有一个 NSButton,我想以编程方式更改其颜色,但我尝试过的任何内容似乎都不起作用。我试图在 NSButtonCell 上创建一个输出并在那里设置背景颜色,但这也不起作用。任何代码片段都会有所帮助。
采纳答案by Alex
The standard Aqua (pill-shaped) buttons are drawn by the system. 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. So Cocoa can't recolor the images to your liking. Only the default button (the one with Return set as its key equivalent) will have a blue pulsing background.
标准的 Aqua(药丸状)按钮由系统绘制。他们没有这样的背景颜色。事实上,它们由 Cocoa 拼接在一起的图像组成,以形成一个连贯的按钮图像。所以 Cocoa 无法根据您的喜好重新着色图像。只有默认按钮(将 Return 设置为其等效键的按钮)将具有蓝色脉冲背景。
What it sounds like you want to do will involve subclassing NSButtonCelland doing your own drawing. If you wanted to recolor the button images to get the effect you want, you can use the excellent Theme Parkutility to extract copies of Apple's button images and use those. 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.
听起来你想要做的将涉及子类化NSButtonCell和做你自己的绘图。如果您想重新着色按钮图像以获得您想要的效果,您可以使用出色的Theme Park实用程序来提取 Apple 按钮图像的副本并使用它们。我承认自己这样做是为了“窃取”某些其他方式无法访问的界面元素,例如 iTunes 中的滚动条。
回答by Mark
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 setBackgroundColordocumentation:
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.
祝你好运。
回答by brianLikeApple
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
回答by Oskar
I've created a NSButtonsubclass called FlatButtonthat makes it super-easy to change button background color, text color, icon color, borders and other types of styling, from Interface Builder:
我创建了一个NSButton名为的子类FlatButton,它可以非常轻松地从 Interface Builder 更改按钮背景颜色、文本颜色、图标颜色、边框和其他类型的样式:
https://github.com/OskarGroth/FlatButton
回答by The iCoder
回答by swift nub
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.
马克的回答是正确的。但是如果通过代码来做对你来说会很麻烦,那么另一种方法是直接在界面构建器中设置它。
- Find the button in interfaceBuilder and click on it.
- 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. Click on it.
- With the NSButtonCell selected, Click on the Identity-Inspector.
- In the Identity-inspector look for the section called
User Defined Runtime Attributes - Click on the little plus (+) sign to add a value. The Value will show to have 3 properties. (A)keyPath (B)Type (C)Value
- Change the keyPath to:
backgroundColor. Change the type to:Color. Change the value to: what ever color you want (a color picker will be displayed)
- 在 interfaceBuilder 中找到按钮并单击它。
- 在界面构建器的对象浏览器部分,您将看到您单击的按钮有一个下拉显示图标。点击它。然后它会显示 NSButton 的 NSButtonCell。点击它。
- 选择 NSButtonCell 后,单击 Identity-Inspector。
- 在身份检查器中查找名为的部分
User Defined Runtime Attributes - 单击小加号 (+) 以添加值。该值将显示具有 3 个属性。(A)keyPath (B)类型(C)值
- 将 keyPath 更改为:
backgroundColor。将类型更改为:Color。将值更改为:您想要的任何颜色(将显示颜色选择器)
Done.
完毕。
回答by GuiDupas
In Swift you can do like this:
在 Swift 中,你可以这样做:
(button.cell as! NSButtonCell).isBordered = false
(button.cell as! NSButtonCell).backgroundColor = .white
回答by A.Badger
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];
}
回答by Philip Fung
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
回答by Lova Rajesh
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]];


