NSTextField - White text on black background, but black cursor

后端 未结 9 1069
温柔的废话
温柔的废话 2020-12-14 16:23

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

9条回答
  •  臣服心动
    2020-12-14 16:38

    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.

提交回复
热议问题