如何在 Objective-C 中子 NSTextView 的焦点上围绕 NSBox 绘制焦点环
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为此,创建 NSTextView 的子类,并像这样覆盖 -becomeFirstResponder :
您可以设置一个 NSNotification 在上面的 if 语句中,以便当该代码块获取时运行后,包含 NSBox 的视图可以被调用,然后在 NSBox 上绘制一个焦点环。要处理 NSTextView 失去焦点,您需要重写 -resignFirstResponder,如下所示:
确保更改界面生成器中的类,并将标头和/或实现文件中的类类型更改为 NSTextView 的新子类:
< img src="https://i.sstatic.net/BkHLc.png" alt="在此处输入图像描述">
To do this, create a subclass of NSTextView, and override the -becomeFirstResponder like so:
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:
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: