NSTextField 在我设置属性字符串占位符时不显示



我正在尝试自定义我的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。第一个不会出现在任何地方。

发生了什么事?

感谢高级。

您将两次设置同一emailTextfield ...

[[[self emailTextField] cell] setPlaceholderAttributedString:emailPlaceholderString];
[[[self emailTextField] cell] setPlaceholderString:@"Password"];

(这是解决问题的问题还是只是问题中的错误?)

最新更新