如何在 Objective-C 中子 NSTextView 的焦点上围绕 NSBox 绘制焦点环

发布于 2024-11-17 17:10:27 字数 109 浏览 2 评论 0原文

我在 NSBox 中有一个 NSTextView 。我想每当 NSTextView 获得焦点时在 NSBox 周围绘制焦点环,并在 NSTextView 失去焦点时立即删除焦点环。

谢谢,

I have one NSTextView within NSBox. I want to draw focus ring aroung NSBox whenever NSTextView got focus and remove focus ring as soon as NSTextView lost focus.

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤千羽 2024-11-24 17:10:27

为此,创建 NSTextView 的子类,并像这样覆盖 -becomeFirstResponder :

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

    return returnValue;
}

您可以设置一个 NSNotification 在上面的 if 语句中,以便当该代码块获取时运行后,包含 NSBox 的视图可以被调用,然后在 NSBox 上绘制一个焦点环。要处理 NSTextView 失去焦点,您需要重写 -resignFirstResponder,如下所示:

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

    }
    return returnValue;
}

确保更改界面生成器中的类,并将标头和/或实现文件中的类类型更改为 NSTextView 的新子类:

< img src="https://i.sstatic.net/BkHLc.png" alt="在此处输入图像描述">

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:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文