【发布时间】:2025-11-30 17:40:01
【问题描述】:
我正在尝试自定义我的NSTextFields,第一步是自定义placeholder。
我想改变占位符的颜色,我是这样尝试的:
- (void)awakeFromNib {
// Color for placeholder in NSTextField - Color: #cdc9c1
NSColor *placeholderColor = [NSColor colorWithCalibratedRed:0.80f green:0.78f blue:0.75f alpha:1.0f];
NSDictionary *placeholderAttributeDict = [NSDictionary dictionaryWithObject:placeholderColor forKey:NSForegroundColorAttributeName];
NSAttributedString *emailPlaceholderString = [[NSAttributedString alloc] initWithString:@"Email" attributes:placeholderAttributeDict];
// NSAttributedString *passPlaceholderString = [[NSAttributedString alloc] initWithString:@"Password" attributes:placeholderAttributeDict];
// NSTextField Email attributes
[[self emailTextField] setDrawsBackground:NO];
[[self emailTextField] setBordered:NO];
[[[self emailTextField] cell] setPlaceholderAttributedString:emailPlaceholderString];
// NSTextField Password attributes
[[self passTextField] setDrawsBackground:NO];
[[self passTextField] setBordered:NO];
[[[self emailTextField] cell] setPlaceholderString:@"Password"];
}
如您所见,第一个 NSTextField 中的第一个占位符是由我尝试指定颜色的 NSAttributedString 建立的。第二个NSTextField 中的第二个占位符只是一个NSString。
当我运行应用程序时,它只显示第二个NSTextField。第一个不会出现在任何地方。
发生了什么?
提前致谢。
【问题讨论】:
标签: objective-c macos customization nstextfield nsattributedstring