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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:53:55  来源:igfitidea点击:

NSTextField placeholder text doesn't show unless editing

objective-ccocoamacosnstextfieldappkit

提问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:

这是我解决这个问题的方法:

  1. Subclass NSTextFieldCell;
  2. Override - (void)drawWithFrame:(NSRect)cellFrame, - (void)drawInteriorWithFrame:(if need) method(s) of NSCelland put all drawing code you need to those methods. Don't forget to call superimplementation;
  3. Set Classfield of NSTextFieldCellin Interface Builderto your subclass;
  4. Setup borderin .xibfile to border(I don't know name of this border);
  5. Turn off Draw Background;
  1. 子类NSTextFieldCell
  2. 覆盖- (void)drawWithFrame:(NSRect)cellFrame- (void)drawInteriorWithFrame:(如果需要)的方法(S) NSCell,把你需要这些方法都绘制代码。不要忘记调用super实现;
  3. 将inClass字段设置为您的子类;NSTextFieldCellInterface Builder
  4. border.xib文件中设置为边界(我不知道此边框的名称);
  5. 关掉Draw Background