NSTextField - White text on black background, but black cursor

后端 未结 9 1064
温柔的废话
温柔的废话 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.

    0 讨论(0)
  • 2020-12-14 16:42

    easiest way is

     override func viewDidAppear() {
        if let fieldEditor = self.view.window?.fieldEditor(true, for: self) as? NSTextView{
            fieldEditor.insertionPointColor = NSColor.black
        }
     }
    
    0 讨论(0)
  • 2020-12-14 16:45

    TextField Insertion Point Color

    NSTextField *textField = self.textField;
    NSColor *insertionPointColor = [NSColor blueColor];
    
    NSTextView *fieldEditor = (NSTextView*)[textField.window fieldEditor:YES
                                                               forObject:textField];
    fieldEditor.insertionPointColor = insertionPointColor;
    
    0 讨论(0)
  • 2020-12-14 16:53

    Your best bet is probably to use NSTextView and - (void)setInsertionPointColor:(NSColor *)color.

    0 讨论(0)
  • 2020-12-14 16:53

    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.

    0 讨论(0)
  • 2020-12-14 16:54

    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
    }
    
    0 讨论(0)
提交回复
热议问题