NSTextField:如何仅在聚焦时绘制背景
我在窗口中放置了一个文本字段,并且我希望文本字段仅在聚焦时绘制背景。 我知道窗口中的所有控件共享一个字段编辑器。 我尝试子类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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最近刚刚在 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 theNSText
/NSTextView
object that is the field editor, andsetBackground:
to configure your color of choice. There are two places to set up a custom field editor.One is to implement
setUpFieldEditorAttributes:
on a customNSTextFieldCell
subclass that you have configured yourNSTextField
to use, and another is to use the window or window delegate methodwindowWillReturnFieldEditor: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.