Anoop Vaidya
Anoop Vaidya

Reputation: 46543

Change FocusRing color of NSTextField, NSTextView

How to change focusRing color from Blue to Red.

(This question is in continuation with Generic validation on input, here instead of setting a default blue focus, I need red one.)

The default looks like this :

enter image description here

I need something like this (Note: The image shown is not perfect as drawn inside Box) :

enter image description here

I tried few things from

  1. http://www.cocoabuilder.com/archive/cocoa/241956-how-to-change-focus-ring-color.html
  2. Cocoa focus ring color animation
  3. http://toomasvahter.wordpress.com/2010/06/13/drawing-focus-rings-in-nscell-and-nsview/

but no success.

Your help is needed.

Upvotes: 4

Views: 1513

Answers (1)

user23790
user23790

Reputation: 563

sub class NSTextview and insert below code

-(void)mouseEntered:(NSEvent *)theEvent{

    CALayer *lay = [self layer];
    CGColorRef  myColor=CGColorCreateGenericRGB(0, 0, 1, 1);
    [lay setBorderColor:myColor];
    [lay setBorderWidth:4];
    //[self setWantsLayer:YES];
    [self setLayer:lay];
    [self makeBackingLayer];
    //CGColorRelease(myColor);
}   

-(void)mouseExited:(NSEvent *)theEvent{

    CALayer *lay = [self layer];
    CGColorRef  myColor=CGColorCreateGenericRGB(0, 0, 1, 1);
    [lay setBorderColor:myColor];
    [lay setBorderWidth:0];
    //[self setWantsLayer:YES];
    [self setLayer:lay];
    [self makeBackingLayer];
    //CGColorRelease(myColor);
}



-(void)updateTrackingAreas{
    [super updateTrackingAreas];
    if (trackingArea){
        [self removeTrackingArea:trackingArea];
        [trackingArea release];
     }

    NSTrackingAreaOptions options = NSTrackingInVisibleRect | NSTrackingMouseEnteredAndExited | NSTrackingActiveInKeyWindow;
    trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect options:options owner:self userInfo:nil];
    [self addTrackingArea:trackingArea];

}  

Upvotes: 1

Related Questions