macos 除非编辑,否则 NSTextField 占位符文本不会显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5519512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSTextField placeholder text doesn't show unless editing
提问by indragie
I set the placeholder text of my NSTextField in Interface Builder, but the placeholder text doesn't show until I click inside the text field to edit it.
我在 Interface Builder 中设置了我的 NSTextField 的占位符文本,但是直到我在文本字段内单击以对其进行编辑时,占位符文本才会显示。
回答by Johann Dirdal
Have you tried setting the placeholder through code? for example something like this:
您是否尝试过通过代码设置占位符?例如这样的事情:
[[textField cell] setPlaceholderString:@"hello"];
回答by GJ Nilsen
Have you bound the NSTextField data in Interface Builder? If so, you have to set the "Multiple Values Placeholder", "No Selection Placeholder" and "Null Placeholder" in the bindings tab in your Utilities inspector. While youre at it, you could set the "No Applicable Placeholder" as well.
您是否在 Interface Builder 中绑定了 NSTextField 数据?如果是这样,您必须在实用程序检查器的绑定选项卡中设置“多值占位符”、“无选择占位符”和“空占位符”。在此期间,您也可以设置“无适用占位符”。
回答by Lexandr
This is how I solve this problem:
这是我解决这个问题的方法:
- Subclass
NSTextFieldCell
; - Override
- (void)drawWithFrame:(NSRect)cellFrame
,- (void)drawInteriorWithFrame:
(if need) method(s) ofNSCell
and put all drawing code you need to those methods. Don't forget to callsuper
implementation; - Set
Class
field ofNSTextFieldCell
inInterface Builder
to your subclass; - Setup
border
in.xib
file to(I don't know name of this border);
- Turn off
Draw Background
;
- 子类
NSTextFieldCell
; - 覆盖
- (void)drawWithFrame:(NSRect)cellFrame
,- (void)drawInteriorWithFrame:
(如果需要)的方法(S)NSCell
,把你需要这些方法都绘制代码。不要忘记调用super
实现; - 将in
Class
字段设置为您的子类;NSTextFieldCell
Interface Builder
border
在.xib
文件中设置为(我不知道此边框的名称);
- 关掉
Draw Background
;