【问题标题】:How to draw focus ring around NSBox on focus of child NSTextView in objective-c如何在Objective-c中子NSTextView的焦点上围绕NSBox绘制焦点环
【发布时间】:2011-06-27 12:58:11
【问题描述】:

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

谢谢,

【问题讨论】:

    标签: cocoa nstextview nsbox focusrect


    【解决方案1】:

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

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

    您可以在上面的 if 语句中设置NSNotification,这样当该代码块运行时,可以调用包含 NSBox 的视图并随后在 NSBox 上绘制焦点环。要处理 NSTextView 失去焦点,您需要覆盖 -resignFirstResponder,如下所示:

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

    一定要更改接口生成器中的类,并将头文件和/或实现文件中的类类型更改为 NSTextView 的新子类:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-05
      • 1970-01-01
      • 2013-07-07
      • 2013-12-31
      • 1970-01-01
      • 2014-05-04
      • 1970-01-01
      相关资源
      最近更新 更多