Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTextField - White text on black background, but black cursor

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 Interface Builder.

The problem I am having is the cursor is black, and hardly visible. Does the cursor not represent the text color? Any ideas how I can fix this?

Otherwise, the NSTextField looks like it cannot be edited.

like image 970
mootymoots Avatar asked Feb 13 '10 16:02

mootymoots


2 Answers

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.

like image 97
uliwitness Avatar answered Nov 06 '22 23:11

uliwitness


TextField Insertion Point Color

NSTextField *textField = self.textField;
NSColor *insertionPointColor = [NSColor blueColor];

NSTextView *fieldEditor = (NSTextView*)[textField.window fieldEditor:YES
                                                           forObject:textField];
fieldEditor.insertionPointColor = insertionPointColor;
like image 28
Zelko Avatar answered Nov 06 '22 22:11

Zelko