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

rob*_*bor 5 macos cocoa objective-c nstextfield

当NSTextField(Label)绑定到带有绑定的控制器选择时,我为多值标记,无选择标记等指定了占位符值,它会绘制一个灰色的文本,在深色背景上不能很好地显示.

有没有办法更改用于显示占位符文本的文本颜色?

Nic*_*ley 15

使用指定所需颜色的属性字符串,如下所示:

NSDictionary *blueDict = [NSDictionary dictionaryWithObject: [NSColor blueColor]
                        forKey: NSForegroundColorAttributeName];
NSAttributedString *blueString = [[[NSAttributedString alloc] initWithString: @"test"
                                attributes: blueDict] autorelease];
Run Code Online (Sandbox Code Playgroud)

然后,您可以直接设置占位符属性字符串:

[[field cell] setPlaceholderAttributedString: blueString];
Run Code Online (Sandbox Code Playgroud)

或者通过绑定来完成,例如:

[field2 bind: @"value" toObject: [NSUserDefaults standardUserDefaults]
        withKeyPath: @"foo"
        options: [NSDictionary dictionaryWithObject: blueString forKey: NSNullPlaceholderBindingOption]];
Run Code Online (Sandbox Code Playgroud)