I\'ve setup an NSTextField with text color as white, and the background color as (black despite not rendering the background color, so its transparent). All in
Since in practice the NSText* returned by -currentEditor for an NSTextField is always an NSTextView*, I added the following code to my custom NSTextField subclass:
-(BOOL) becomeFirstResponder
{
BOOL success = [super becomeFirstResponder];
if( success )
{
// Strictly spoken, NSText (which currentEditor returns) doesn't
// implement setInsertionPointColor:, but it's an NSTextView in practice.
// But let's be paranoid, better show an invisible black-on-black cursor
// than crash.
NSTextView* textField = (NSTextView*) [self currentEditor];
if( [textField respondsToSelector: @selector(setInsertionPointColor:)] )
[textField setInsertionPointColor: [NSColor whiteColor]];
}
return success;
}
So if you're already replacing this class because you're doing custom background drawing, this might be a more encapsulated solution. Maybe there's even a way to move this up into NSCell, which would be cleaner since NSCell is the one doing the drawing and knowing the colors anyway.
easiest way is
override func viewDidAppear() {
if let fieldEditor = self.view.window?.fieldEditor(true, for: self) as? NSTextView{
fieldEditor.insertionPointColor = NSColor.black
}
}
NSTextField *textField = self.textField;
NSColor *insertionPointColor = [NSColor blueColor];
NSTextView *fieldEditor = (NSTextView*)[textField.window fieldEditor:YES
forObject:textField];
fieldEditor.insertionPointColor = insertionPointColor;
Your best bet is probably to use NSTextView and - (void)setInsertionPointColor:(NSColor *)color.
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.
Swift 4 Solution
override func viewDidAppear() {
super.viewDidAppear()
guard let window = _textField.window, let fieldEditor = window.fieldEditor(true, for: _textField) as? NSTextView else { return }
fieldEditor.insertionPointColor = .white
}