28

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

我遇到的问题是光标是黑色的,几乎看不到光标不代表文本颜色吗?有什么想法可以解决这个问题吗?

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

4

10 回答 10

36

由于实际上 -currentEditor 为 NSTextField 返回的 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;
}

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

于 2012-02-02T12:01:49.957 回答
28

文本字段插入点颜色

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

NSTextView *fieldEditor = (NSTextView*)[textField.window fieldEditor:YES
                                                           forObject:textField];
fieldEditor.insertionPointColor = insertionPointColor;
于 2012-04-29T21:47:36.227 回答
20

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

于 2010-02-13T16:53:13.777 回答
14

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

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

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

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

于 2010-02-14T03:21:20.707 回答
5

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

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

对于Swift开发人员:

将 insertPointColor 方法设置为扩展:

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

并打电话

 override func viewDidAppear() {
        super.viewDidAppear()
        textField.customizeCursorColor(NSColor.red)
    }
于 2017-08-01T09:21:18.913 回答
3

斯威夫特 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
}
于 2018-07-10T22:16:59.233 回答
2

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

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

@implementation AppDelegate

@synthesize password = m_password;

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

然后我创建了一个自定义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

注意:虽然,我不明白为什么当我在派生类中执行此操作时无法设置背景颜色,例如使用textColor. 这将允许摆脱IBOutlet和成员变量。

于 2012-01-03T14:15:49.713 回答
0

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

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)
    }
}

于 2019-09-19T07:14:05.613 回答
0

最简单的方法是

 override func viewDidAppear() {
    if let fieldEditor = self.view.window?.fieldEditor(true, for: self) as? NSTextView{
        fieldEditor.insertionPointColor = NSColor.black
    }
 }
于 2020-11-06T12:39:32.723 回答
0

我迅速使用此代码

 if let editor = textField.currentEditor() as? NSTextView{
     editor.insertionPointColor = myColor ?? .black
 }
于 2021-08-26T07:21:38.597 回答