当焦点位于字段上时删除的 NSTextField / NSSearchField 默认值
我希望为 NSTextField
(或 NSSearchField
)设置一个默认值,并在用户单击它时删除该默认值,并在文本视图丢失时设置该默认值。焦点且文本字段为空。像这样:
听起来我应该扩展 NSSearchField
但我想知道是否有更简单的解决方案。
I would like to have a default value for a NSTextField
(or NSSearchField
) and have this default value removed when the user clicks on it and set back when the text view loses the focus and the text field is empty. Like this:
It sounds like I should extend NSSearchField
but I was wondering if there was an easier solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该功能实际上是内置的;无需子类化或以其他方式扩展
NSSearchField
。这是该字段的 占位符字符串。您可以在 IB 中或通过调用
setPlaceholderString:
。请注意,这是NSTextFieldCell
的一个方法。 NSTextField 是其单元格的“公共面孔”,并且具有几乎所有单元格功能的覆盖方法。但是,在这种情况下,您需要将消息直接发送到单元格:因为
NSSearchField
和NSSearchFieldCell
继承自NSTextField
和NSTextFieldCell
分别,他们的过程是相同的。This functionality is in fact built-in; there's no need to subclass or otherwise extend
NSSearchField
.That's the field's placeholder string. You can set it either in IB or via a call to
setPlaceholderString:
. Notice that this is a method ofNSTextFieldCell
. AnNSTextField
is the "public face" of its cell, and has cover methods for almost all of the cell's functionality. In this case, however, you need to send the message directly to the cell:Since
NSSearchField
andNSSearchFieldCell
inherit fromNSTextField
andNSTextFieldCell
, respectively, the process is the same for them.设置 NSTextFields 的 NSTextFieldCell 上的占位符字符串
使用
[[textField cell] setPlaceholderString:@"String Matching"];
Set the place holder string on the NSTextFields's NSTextFieldCell using
[[textField cell] setPlaceholderString:@"String Matching"];