简体   繁体   English

如何在显示占位符标记时控制NSTextField的文本颜色?

[英]How to control the text color of an NSTextField when it is displaying a placeholder marker?

When the NSTextField (Label) is bound to a controller selection with bindings, and I have specified placeholder values for the Multiple Values Marker, No Selection Marker, etc it draws the text with a gray color that does not show up well on a dark background. 当NSTextField(Label)绑定到带有绑定的控制器选择时,我为多值标记,无选择标记等指定了占位符值,它会在深色背景上绘制一个灰色的文本。

Is there a way to change the text color it uses to display the placeholder text? 有没有办法更改用于显示占位符文本的文本颜色?

Use an attributed string specifying the color you want, like this: 使用指定所需颜色的属性字符串,如下所示:

NSDictionary *blueDict = [NSDictionary dictionaryWithObject: [NSColor blueColor]
                        forKey: NSForegroundColorAttributeName];
NSAttributedString *blueString = [[[NSAttributedString alloc] initWithString: @"test"
                                attributes: blueDict] autorelease];

Then you can either set the placeholder attributed string directly: 然后,您可以直接设置占位符属性字符串:

[[field cell] setPlaceholderAttributedString: blueString];

or do it through a binding, for example: 或者通过绑定来完成,例如:

[field2 bind: @"value" toObject: [NSUserDefaults standardUserDefaults]
        withKeyPath: @"foo"
        options: [NSDictionary dictionaryWithObject: blueString forKey: NSNullPlaceholderBindingOption]];

you can try to make your own "palceholder". 你可以尝试制作自己的“掌控者”。 I mean your can show a label over your textfield with any text patameters you want. 我的意思是你可以用你想要的任何文字参数在你的文本区域上显示一个标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM