Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set background color of NSButton OSX

I want to set the background color of NSButton.There was nothing in the attribute inspector so i was wondering if there was any way to do it programmatically?

like image 569
Zeist Avatar asked Apr 01 '15 09:04

Zeist


2 Answers

System controls will need to follow the Apple look&feel, so you cannot easily change the background colour. If you want to accomplish this, you'll need to subclass NSButton and overwrite the drawRect: method. The downside is that you'll also need to handle the text drawing, and possibly different rendering based on button state.

Edit. Actually, you'll need to subclass the NSButtonCell class for the drawing stuff, more info can be found here: https://developer.apple.com/library/prerelease/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSButtonCell_Class/index.html#//apple_ref/doc/uid/20000093-SW15

like image 166
Cristik Avatar answered Oct 07 '22 13:10

Cristik


Other than subclassing you can use layer approach like this

NSButton *button = [[NSButton alloc] initWithFrame:frame];
[superView addSubView:button];
[button setWantsLayer:YES];
button.layer.backgroundColor = [NSColor blueColor].CGColor;

or you can make an image with that specified color and apply that image to the Button.

like image 35
Sheen Vempeny Avatar answered Oct 07 '22 14:10

Sheen Vempeny