开发者

How to draw focus ring around NSBox on focus of child NSTextView in objective-c

开发者 https://www.devze.com 2023-03-16 02:49 出处:网络
I have one NSTextView within NSBox. I want to draw focus ring aroung NSBox whenever NSTextView got focus a开发者_StackOverflownd remove focus ring as soon as NSTextView lost focus.

I have one NSTextView within NSBox. I want to draw focus ring aroung NSBox whenever NSTextView got focus a开发者_StackOverflownd remove focus ring as soon as NSTextView lost focus.

Thanks,


To do this, create a subclass of NSTextView, and override the -becomeFirstResponder like so:

- (BOOL)becomeFirstResponder
{
    BOOL returnValue = [super becomeFirstResponder];
    if (returnValue) {
        //do something here when this becomes first responder
    }

    return returnValue;
}

You can set up an NSNotification in the if statement above so that when that code block gets run, your view containing the NSBox can get called and subsequently draw a focus ring on the NSBox. To handle the NSTextView losing focus, you'll want to override -resignFirstResponder, like so:

- (BOOL)resignFirstResponder
{
    BOOL returnValue = [super resignFirstResponder];
    if(returnValue){
        //do something when resigns first responder

    }
    return returnValue;
}

Be sure to change the class in interface builder, and change your class type in your header and/or implementation files to your new subclass of NSTextView:

How to draw focus ring around NSBox on focus of child NSTextView in objective-c

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号