Inspired by the great answer of Jon Steinmetz I created the following example.
I added a NSSecureTextField
to the application view and connected it to the IBOutlet
of the member variable I placed into AppDelegate
.
@implementation AppDelegate
@synthesize password = m_password;
- (void)awakeFromNib {
assert(m_password);
self.password.backgroundColor = [NSColor blackColor];
}
Then I created a custom NSSecureTextField
class. I noticed that is in some cases not enough to set the colors in awakeFromNib
but I cannot give a reason for this.
@implementation CustomSecureTextField
- (void)customize {
// Customize the text and caret color.
NSColor* foregroundColor = [NSColor whiteColor];
self.textColor = foregroundColor;
[[self.cell fieldEditorForView:self] setInsertionPointColor:foregroundColor];
}
- (void)awakeFromNib {
[self customize];
}
- (void)textDidBeginEditing:(NSNotification*)notification {
// Called when the user inputs a character.
[self customize];
}
- (void)textDidEndEditing:(NSNotification*)notification {
// Called when the user clicks into the field for the first time.
[self customize];
}
- (void)textDidChange:(NSNotification*)notification {
// Just in case ... for the paranoid programmer!
[self customize];
}
@end
Note: Though, I do not understand why the background color cannot be set when I do this in the derived class like with the textColor
. That would allow to get rid of the IBOutlet
and the member variable.