当我设置AttributedString占位符时,NSTextField没有显示

时间:2012-12-20 04:25:41

标签: objective-c macos customization nstextfield nsattributedstring

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

发生了什么事?

先谢谢。

1 个答案:

答案 0 :(得分:1)

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

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

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

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