![](/img/trans.png)
[英]Rendering differences between NSTextField and NSTextView on a black background
[英]NSTextField - White text on black background, but black cursor
我已经设置了一个NSTextField
,文本颜色为白色,背景色为(黑色,尽管没有渲染背景色,所以它是透明的)。 全部在界面生成器中。
我遇到的问题是光标是黑色的,几乎不可见。 光标不代表文本颜色吗? 有什么想法可以解决这个问题吗?
否则, NSTextField
看起来无法编辑。
由于实际上 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;
}
因此,如果您因为要进行自定义背景绘图而已经替换了此类,那么这可能是一个更封装的解决方案。 也许甚至有一种方法可以将其移动到 NSCell 中,这会更清晰,因为 NSCell 是进行绘图并知道颜色的人。
NSTextField *textField = self.textField;
NSColor *insertionPointColor = [NSColor blueColor];
NSTextView *fieldEditor = (NSTextView*)[textField.window fieldEditor:YES
forObject:textField];
fieldEditor.insertionPointColor = insertionPointColor;
您最好的选择可能是使用 NSTextView 和- (void)setInsertionPointColor:(NSColor *)color
。
假设您要设置插入符号的颜色而不是鼠标光标,那么使用setInsertionPointColor:
的建议应该可行。
但是,您不一定需要从使用NSTextField
更改为NSTextView
。 NSTextField
所在窗口的字段编辑器是一个NSTextView
。 因此,当您的NSTextField
成为关键视图时,您可以抓取字段编辑器并调用setInsertionPointColor:
。 当您的字段不再是关键视图时,您可能需要重置颜色。
您可以使用NSWindow
的fieldEditor:forObject:
或NSCell
的fieldEditorForView:
来获取字段编辑器。
如果您有 NSTextField 的子类,您可以让它使用 NSTextFieldCell 的自定义子类并覆盖-(NSText*)setUpFieldEditorAttributes:(NSText*)textObj
。 在该方法中,您可以设置插入点颜色一次,并且在该文本字段的字段编辑器处于活动状态时它将保持不变。 虽然当字段编辑器移动到另一个编辑字段时,插入点颜色将保持不变,除非您重置它。
我在viewDidLoad
和应用程序崩溃中调用了insertionPointColor
viewDidLoad
。
我通过在viewDidAppear
上调用insertionPointColor
点viewDidAppear
解决这个问题。
对于Swift开发人员:
将插入点颜色方法设置为扩展:
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)
}
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
}
受到Jon Steinmetz出色回答的启发,我创建了以下示例。
我在应用程序视图中添加了一个NSSecureTextField
并将其连接到我放入AppDelegate
的成员变量的IBOutlet
。
@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
和成员变量。
最简单的方法是
override func viewDidAppear() {
if let fieldEditor = self.view.window?.fieldEditor(true, for: self) as? NSTextView{
fieldEditor.insertionPointColor = NSColor.black
}
}
如果使用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)
}
}
我迅速使用此代码
if let editor = textField.currentEditor() as? NSTextView{
editor.insertionPointColor = myColor ?? .black
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.