NSTextField:如何仅在聚焦时绘制背景

发布于 2024-11-09 00:22:21 字数 1278 浏览 2 评论 0原文

我在窗口中放置了一个文本字段,并且我希望文本字段仅在聚焦时绘制背景。 我知道窗口中的所有控件共享一个字段编辑器。 我尝试子类nstextfield并实现becomeFirstResponder和resignFirstResponder。 并尝试在窗口中使用自定义单例编辑器。

有人知道如何实现这一目标吗?


在 NSWindow 中,每个文本字段或按钮共享一个字段编辑器实例(一个单例 NSTextView 实例),因此当您单击文本字段时,文本字段首先成为第一个响应者,然后快速将其传递给共享字段编辑器。因此,当文本字段失去焦点时,文本字段的 resignFirstResponder 永远不会被调用(因为字段编辑器现在是 FirstResponder)。

您可以查看 NSWindow API 中的 fieldEditor:forObject: 。 http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSWindow_Class/Reference/Reference.html#//apple_ref/occ/instm/NSWindow /fieldEditor:forObject:


解决方案: (谢谢,迈克尔戈尔巴赫) 在我的窗口控制器中

- (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject
{
    NSText *text = [sender fieldEditor:YES forObject:self];
    if(text&&[anObject isKindOfClass:[MyCustomTextField class]])
    {
        [text setBackgroundColor:[NSColor whiteColor]];
        [text setDrawsBackground:YES];
    }
    return text;
}

I put a textfield in a window, and I want the textfield draw background only when focused.
I know that all the controls in the window share one field editor.
I tried subclass nstextfield and implement becomeFirstResponder and resignFirstResponder.
And tried use custom singleton editor for the window .

Any one know how to achieve this?


In the NSWindow ,every textfield or button share one instance of field editor(a singleton NSTextView instance),so when you click the textfield, textfield become firstResponser first,and then quickly pass it to the shared field editor. So when the textfield lost focus ,the resignFirstResponder of the textfield will never be called(because the field editor is the FirstResponder now).

You can look at fieldEditor:forObject: in NSWindow API.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSWindow_Class/Reference/Reference.html#//apple_ref/occ/instm/NSWindow/fieldEditor:forObject:


SOLUTION:
(Thanks , Michael Gorbach)
In my window controller

- (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject
{
    NSText *text = [sender fieldEditor:YES forObject:self];
    if(text&&[anObject isKindOfClass:[MyCustomTextField class]])
    {
        [text setBackgroundColor:[NSColor whiteColor]];
        [text setDrawsBackground:YES];
    }
    return text;
}

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

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

发布评论

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

评论(1

为人所爱 2024-11-16 00:22:21

我最近刚刚在 tableView 中做到了这一点。您需要使用自定义单元格和字段编辑器。具体来说,您需要在作为字段编辑器的 NSText/NSTextView 对象上调用 setDrawsbackground:YES,并调用 setBackground: code> 配置您选择的颜色。有两个地方可以设置自定义字段编辑器。

一种是在您已配置要使用的 NSTextField 的自定义 NSTextFieldCell 子类上实现 setUpFieldEditorAttributes:,另一种是使用窗口或窗口委托方法windowWillReturnFieldEditor:toObject:

请注意,如果第一种方法不适用于特定设置,有时您需要使用第二种方法,因为它在代码路径中较早进入。

I just did this recently, in a tableView. You need to use a custom cell and fieldEditor. Specifically, you need to call setDrawsbackground:YES on the NSText/NSTextView object that is the field editor, and setBackground: to configure your color of choice. There are two places to set up a custom field editor.

One is to implement setUpFieldEditorAttributes: on a custom NSTextFieldCell subclass that you have configured your NSTextField to use, and another is to use the window or window delegate method windowWillReturnFieldEditor:toObject:.

Note that if the first method doesn't work for a particular setting, sometimes you need to use the second, because it gets in earlier in the codepath.

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