0
votes

I have a NSTextField with rounded corners generated by subclassing NSTextFieldCell and overriding

(void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView

With the following

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {

    controlView.layer.cornerRadius = cellFrame.size.height / 2;
    [super drawInteriorWithFrame:cellFrame inView:controlView];
} 

This seems to work fine, however when the textfield is first responder the focus ring still appears as a rectangle shape. Is it possible modify the focus ring to correspond to the shape of the NSTextField?

2

2 Answers

1
votes

Yes, I have done this; however it has been a while so it may take a few edits :)

I believe you have 2 options:

  1. You can set focusRingType to NSFocusRingTypeNone then after [super drawInteriorWithFrame:] you can draw your own focus ring using something like [NSBezierPath bezierPathWithRoundedRect:].
  2. You can override a private method called (I think) _drawFocusRingWithFrame

either way you will be drawing the ring yourself, but that part is easy. If you desire the system focus ring color use [NSColor keyboardFocusIndicatorColor] for the stroke color of your path.

On 10.7 or later there is a method called drawFocusRingMaskWithFrame on NSCell.

2
votes

You can override - (void)drawFocusRingMaskWithFrame:inView: and draw your own in NSTextFieldCell

It should be pretty easy to do by creating a path with your view's frame and adding some corner radius. Remember to use [NSColor keyboardFocusIndicatorColor] so it matches the user's selection color.