I have a borderless NSButton
(more or less a NSTextField
with an action) that I would like to behave like an "image-based" button, i.e. I would like the button text to change color when passed over with a mouse and when clicked. Is there an easy way to achieve that ?
Here is my button initialization context :
NSButton* but =[[NSButton alloc] initWithFrame:NSMakeRect(5, 5, 125, 25)] ;
[but setTitle:@"Do not show Again"];
[but setButtonType:NSMomentaryLightButton]; //I figure it could be here
[but setBezelStyle:NSHelpButtonBezelStyle];
[but setBordered:NO];
[but setAction:@selector(writeToUserDefaults)];
[but setFont:[NSFont fontWithName:@"Arial" size:9.0]];
[self.view addSubview:but];
Source that didn't work : Borderless NSButton Turns Gray
What are my solution (besides subclassing NSButton ?)
The solution is to use NSTrackingArea, from Apple documentation
An NSTrackingArea object defines a region of view that generates mouse-tracking and cursor-update events when the mouse is over that region.
In your situation you need to set up a tracking area with respect to the frame of your NSButton and change text color when you receive mouse tracking events.
- (void)awakeFromNib:(NSWindow *)newWindow
{
NSButton* but =[[NSButton alloc] initWithFrame:NSMakeRect(5, 5, 125, 25)] ;
[but setTitle:@"Do not show Again"];
[but setBezelStyle:NSHelpButtonBezelStyle];
[but setBordered:NO];
[but setAction:@selector(writeToUserDefaults)];
[but setFont:[NSFont fontWithName:@"Arial" size:9.0]];
[self.view addSubview:but];
NSTrackingArea* trackingArea = [[NSTrackingArea alloc] initWithRect:[but frame]
options:(NSTrackingInVisibleRect | NSTrackingEnabledDuringMouseDrag | NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways)
owner:self userInfo:nil];
[self.view addTrackingArea:trackingArea];
}
- (void) mouseEntered:(NSEvent*)theEvent {
//Set text color
}
- (void) mouseExited:(NSEvent*)theEvent {
//Reset text color
}