3

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

Abcd Efg
  • 2,146
  • 23
  • 41

2 Answers2

1

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.

DarkDust
  • 90,870
  • 19
  • 190
  • 224
  • I couldn't get this to work at all - those methods weren't called so I had to revert to pre-10.7 ways of drawing the focus ring. There doesn't appear to be any help about it either on the net. – trojanfoe Oct 22 '13 at 20:51
-2

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
}
Ramaraj T
  • 5,184
  • 4
  • 35
  • 68
  • I think, there is no other way than this. Or you can try like this: http://stackoverflow.com/questions/12118932/nsbutton-image-shadow-on-mouseentered-event/12802496#12802496 – Ramaraj T Oct 11 '12 at 12:52
  • 1
    You didn't fully understand my question. I asked for Focus Ring. It is different from hover effect. – Abcd Efg Oct 12 '12 at 11:32