Re: Changing the "on" color of an NSButton
Re: Changing the "on" color of an NSButton
- Subject: Re: Changing the "on" color of an NSButton
- From: Eric Ridge <email@hidden>
- Date: Mon, 28 Jan 2002 01:05:34 -0500
On Saturday, January 19, 2002, at 02:17 PM, Eric Ridge wrote:
>
Question:
>
Is it possible to change the "on" color of a Round On/Off (or any)
>
Button?
Hi! I asked the above last week, and didn't receive the easy answer I
was hoping for. So, attached is what I hope will be an easy answer for
others that want colored buttons.
I've spent a few hours here and there working on an NSButtonCell
subclass (ColoredButtonCell) that allows you to create colored buttons
(Round, Square, Push, On/Off, Toggle, etc. Everything 'cept check and
radio buttons). It's not perfect, but it's pretty damn good, if I do
say so myself. :) The great thing is that it doesn't require external
images. However, it does not support "throbbing" of default buttons,
but it does everything else (as far as I can tell).
1) create your buttons as usual in InterfaceBuilder
2) connect them to a controller outlet
3) In the controller (awakeFromNib), do this on each button you want to
colorize:
[myButton
setCell: [ColoredButtonCell
cellForButton:myButton
withColor:[NSColor xxxColor]
]
];
Since you can't specify the cell type in IB (and since I couldn't get IB
to understand the NSButton subclass I started with), I tried to make
using the cell as easy as possible.
So here it is. Use it if you like, free of charge and warranty! Please
feel free to pass constructive criticism back my way.
thanks!
eric
ps, the .tiff is an example of what some small round buttons look like
with color.
/* ColoredButtonCell */
#import <Cocoa/Cocoa.h>
/**
* 1) create your buttons as usual in InterfaceBuilder
* 2) connect them to a controller outlet
* 3) In the controller (awakeFromNib), do this on each button you want to colorize:
*
* [myButton
* setCell: [ColoredButtonCell
* cellForButton:myButton
* withColor:[NSColor xxxColor]
* ]
* ];
*/
@interface ColoredButtonCell : NSButtonCell
{
@private
NSImage *_button, *_overlay, *_result, *_mask, *_shadowMask;
NSSize _sourceSize;
NSRect _sourceFrame, _imageRect;
NSColor *_color;
NSButtonCell *_dummy;
int _currentState;
BOOL _mouseDown;
BOOL _colorizeWhenDeselected;
}
/**
* simple factory method for creating a ColoredButtonCell
* that is configured to work with the specified button. Also
* sets the color.
*/
+ (id)cellForButton:(NSButton *)button withColor:(NSColor *)color;
/** the color of the cell */
- (NSColor *)color;
/** change the color of the button */
- (void)setColor:(NSColor *)color;
/** should the cell colorize itself even when not selected */
- (BOOL)colorizeWhenDeselected;
/** change the state of the "colorizeWhenDeselected" flag */
- (void)setColorizeWhenDeselected:(BOOL)colorize;
@end
[demime 0.98b removed an attachment of type application/octet-stream which had a name of ColoredButtonCell.m]
[demime 0.98b removed an attachment of type image/tiff which had a name of ColoredButtonCell.tiff]