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
NSTextField - White text on black background, but black cursor
提问by mootymoots
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 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 NSTextField
looks 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 NSTextField
to NSTextView
. The field editor for window that the NSTextField
is in is an NSTextView
. So when your NSTextField
becomes 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 更改NSTextField
为NSTextView
。所在窗口的字段编辑器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 insertionPointColor
in viewDidLoad
and app crashes.
我打insertionPointColor
了电话viewDidLoad
,应用程序崩溃了。
I fixed this by calling insertionPointColor
on 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 NSSecureTextField
to the application view and connected it to the IBOutlet
of 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 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.
然后我创建了一个自定义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 IBOutlet
and 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 methodInvoked
as 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)
}
}