macos NSTextField - 黑色背景上的白色文本,但黑色光标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2258300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:45:47  来源:igfitidea点击:

NSTextField - White text on black background, but black cursor

objective-cmacoscocoacustomizationnstextfield

提问by mootymoots

I've setup an NSTextFieldwith text color as white, and the background color as (black despite not rendering the background color, so its transparent). All in Interface Builder.

我已经设置了一个NSTextField文本颜色为白色,背景颜色为(黑色尽管没有渲染背景颜色,所以它是透明的)。全部在界面生成器中。

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 NSTextFieldlooks like it cannot be edited.

否则,NSTextField看起来无法编辑。

采纳答案by Robert Karl

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

您最好的选择可能是使用 NSTextView 和- (void)setInsertionPointColor:(NSColor *)color.

回答by uliwitness

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:

由于实际上 NSTextField 由 -currentEditor 返回的 NSText* 始终是 NSTextView*,因此我将以下代码添加到我的自定义 NSTextField 子类中:

-(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.

因此,如果您因为要进行自定义背景绘图而已经替换了此类,那么这可能是一个更加封装的解决方案。也许甚至有一种方法可以将其移动到 NSCell 中,这会更清晰,因为 NSCell 是进行绘图并知道颜色的人。

回答by Zelko

TextField Insertion Point Color

文本字段插入点颜色

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

NSTextView *fieldEditor = (NSTextView*)[textField.window fieldEditor:YES
                                                           forObject:textField];
fieldEditor.insertionPointColor = insertionPointColor;

回答by Jon Steinmetz

Assuming that you are wanting to set the color of the insertion caret and not the mouse cursor then the suggestion of using setInsertionPointColor:should work.

假设您要设置插入符号的颜色而不是鼠标光标,那么使用的建议setInsertionPointColor:应该可行。

However, you do not necessarily need to change from using NSTextFieldto NSTextView. The field editor for window that the NSTextFieldis in is an NSTextView. So when your NSTextFieldbecomes the key view you could grab the field editor and call setInsertionPointColor:on that. You may need to reset the color when your field stops being the key view.

但是,您不一定需要从 using 更改NSTextFieldNSTextView。所在窗口的字段编辑器NSTextField是一个NSTextView. 因此,当您NSTextField成为关键视图时,您可以获取字段编辑器并调用setInsertionPointColor:它。当您的字段不再是关键视图时,您可能需要重置颜色。

You can get the field editor by using NSWindow's fieldEditor:forObject:or NSCell's fieldEditorForView:.

您可以使用NSWindow'sfieldEditor:forObject:NSCell's来获取字段编辑器fieldEditorForView:

If you have a subclass of NSTextField you can have it use a custom subclass of NSTextFieldCell and override -(NSText*)setUpFieldEditorAttributes:(NSText*)textObj. In that method you can set the insertion point color once and it will stay while the field editor is active for this text field. Though when the field editor is moved to another edit field the insertion point color will remain unless you reset it.

如果您有 NSTextField 的子类,您可以让它使用 NSTextFieldCell 的自定义子类并覆盖-(NSText*)setUpFieldEditorAttributes:(NSText*)textObj。在该方法中,您可以设置插入点颜色一次,并且在该文本字段的字段编辑器处于活动状态时它将保持不变。虽然当字段编辑器移动到另一个编辑字段时,插入点颜色将保持不变,除非您重置它。

回答by Gent Berani

I've called insertionPointColorin viewDidLoadand app crashes.

我打insertionPointColor了电话viewDidLoad,应用程序崩溃了。

I fixed this by calling insertionPointColoron viewDidAppear.

我通过调用解决了这个insertionPointColor问题viewDidAppear

For Swiftdevelopers:

对于Swift开发人员:

Set insertionPointColor method into extension:

将插入点颜色方法设置为扩展:

extension NSTextField {
    public func customizeCursorColor(_ cursorColor: NSColor) {
        let fieldEditor = self.window?.fieldEditor(true, for: self) as! NSTextView
        fieldEditor.insertionPointColor = cursorColor
    }
}

and call

并打电话

 override func viewDidAppear() {
        super.viewDidAppear()
        textField.customizeCursorColor(NSColor.red)
    }

回答by Charlton Provatas

Swift 4 Solution

Swift 4 解决方案

override func viewDidAppear() {
    super.viewDidAppear()
    guard let window = _textField.window, let fieldEditor = window.fieldEditor(true, for: _textField) as? NSTextView else { return }
    fieldEditor.insertionPointColor = .white
}

回答by JJD

Inspired by the great answer of Jon SteinmetzI created the following example.

受到Jon Steinmetz出色回答的启发,我创建了以下示例。

I added a NSSecureTextFieldto the application view and connected it to the IBOutletof the member variable I placed into AppDelegate.

我将 a 添加NSSecureTextField到应用程序视图并将其连接到IBOutlet我放入的成员变量的AppDelegate

@implementation AppDelegate

@synthesize password = m_password;

- (void)awakeFromNib {
    assert(m_password);
    self.password.backgroundColor = [NSColor blackColor];
}

Then I created a custom NSSecureTextFieldclass. I noticed that is in some cases not enough to set the colors in awakeFromNibbut I cannot give a reason for this.

然后我创建了一个自定义NSSecureTextField类。我注意到在某些情况下这不足以设置颜色,awakeFromNib但我无法给出原因。

@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 IBOutletand the member variable.

注意:虽然,我不明白为什么当我在派生类中像textColor. 这将允许摆脱IBOutlet和 成员变量。

回答by duan

If you use Objective-C runtime selector capture combined with uliwitness's solution, you can achieve it without subclassing NSTextField, here I use RxCocoa's methodInvokedas an example:

如果你使用Objective-C运行时选择器捕获结合uliwitness的解决方案,你可以在不继承NSTextField的情况下实现它,这里我以RxCocoa的methodInvoked为例:

import Cocoa
import RxCocoa

extension NSTextField {
    func withCursorColor(_ color: NSColor) {
        rx.methodInvoked(#selector(becomeFirstResponder))
            .subscribe(onNext: { [unowned self] _ in
                guard let editor = self.currentEditor() as? NSTextView else { return }
                editor.insertionPointColor = color
            })
            .disposed(by: rx.disposeBag)
    }
}