0

我正在尝试自定义一点 my 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. 第一个没有出现在任何地方。

怎么了?

提前致谢。

4

1 回答 1

1

您正在设置相同的 emailTextField 两次...

[[[self emailTextField] cell] setPlaceholderAttributedString:emailPlaceholderString];

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

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

于 2012-12-20T04:29:09.967 回答