I've subclassed a NSButton and NSButtonCell, I've changed the drawings. Now the focus ring no longer appears on the button. I'm looking for a way to draw a focus ring with a custom color, on my custom button.
Question Updated
I've subclassed a NSButton and NSButtonCell, I've changed the drawings. Now the focus ring no longer appears on the button. I'm looking for a way to draw a focus ring with a custom color, on my custom button.
Question Updated
I'm not aware of how to draw the focus ring yourself (so you do a custom color), but to have your custom button cell draw the focus ring you usually only need to override focusRingMaskBoundsForFrame:inView: like this:
- (NSRect)focusRingMaskBoundsForFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
return cellFrame;
}
If that doesn't give you the result you wanted, you might need to additionally provide a mask by implementing drawFocusRingMaskWithFrame:inView:. In this method you basically just draw what should get a focus ring added. Cocoa does the rest for you.
Note that these methods only work for Mac OS X 10.7 Lion and higher, but this is how Apple says it should be done now.
You can add Tracking area for your custom button class and then you can customize your button focus.
-(void)awakeFromNib{
int opts = (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways);
NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:self.frame options:opts owner:self userInfo:nil];
[self addTrackingArea:trackingArea];
}
Now you can do your customization in the following functions.
- (void) mouseEntered:(NSEvent*)event{
//Add Custom Focus Ring
}
- (void) mouseExited:(NSEvent*)event{
//Remove Custom Focus Ring
}