简体   繁体   English

如何更改NSTableCellView中NSTextField的突出显示颜色?

[英]How can you change the highlight color of a NSTextField within a NSTableCellView?

In storyboard when I set the Text Color of a NSTextField within a NSTableCellView to Control Text Color the color becomes black and when the cell is selected/highlighted it will become white. 在情节NSTableCellView ,当我将NSTableCellView中的NSTextField的文本颜色设置为Control Text Color ,颜色变为黑色,而选中/突出显示该单元格的颜色将变为白色。 When you deselect the color will return to black. 取消选择时,颜色将恢复为黑色。 You get all this behavior for free. 您可以免费获得所有这些行为。

I created another textfield under the default it is similar to that of iOS (see the label Game in the image below). 我在默认情况下创建了另一个文本字段,该字段与iOS相似(请参见下图中的标签Game)。 I changed the Text Color to the gray and that all works. 我将“文字颜色”更改为灰色,并且一切正常。 However, when I now select/highlight the cell, the color remains gray and does not swap to white. 但是,当我现在选择/突出显示该单元格时,颜色仍为灰色,并且不会交换为白色。 How can I get such behavior through Storyboard? 如何通过情节提要获得这种行为? And if not possible, through code? 如果不可能,通过代码?

在此处输入图片说明

There is no automatic way to get what you want. 没有自动获得所需内容的方法。 The automatic behavior you're seeing with the black label is implemented by NSTextFieldCell (or one of its superclasses, like NSCell ). 您看到的带有黑色标签的自动行为是由NSTextFieldCell (或其超类之一,如NSCell )实现的。 It is triggered by the setting of the cell's backgroundStyle to NSBackgroundStyleDark . 它是通过将单元格的backgroundStyleNSBackgroundStyleDark The cell's backgroundStyle is set by NSTableCellView when its own backgroundStyle is set. 细胞的backgroundStyle被设置NSTableCellView当自己的backgroundStyle设置。 NSTableCellView 's backgroundStyle is set by NSTableRowView when its interiorBackgroundStyle changes, which happens when its other properties, like selected and emphasized , are set. NSTableRowViewinteriorBackgroundStyle更改时, NSTableCellViewbackgroundStyle会由NSTableRowView设置,而在设置其其他属性(如selectedemphasized时会发生这种情况。

Anyway, the cell only automatically changes the color it uses to draw if its textColor is [NSColor controlTextColor] or has equivalent RGB values. 无论如何,如果单元格的textColor[NSColor controlTextColor]或具有相等的RGB值,则该单元格只会自动更改其绘制的颜色。 So, it doesn't work for your gray labels. 因此,它不适用于您的灰色标签。

You could implement a custom subclass of NSTableCellView or NSTextField . 您可以实现NSTableCellViewNSTextField的自定义子类。 Your class would implement (override, for a subclass of NSTableCellView ) -setBackgroundStyle: . 您的类将实现(对于NSTableCellView的子类NSTableCellView-setBackgroundStyle: In your method, you can check what style is being set and change the text field's textColor . 在您的方法中,您可以检查正在设置的样式并更改文本字段的textColor If it's an override, call through to super. 如果它是替代,请调用super。 (Although NSTextField does not currently implement a backgroundStyle property, it probably will in the future. Apple has said they will be adding cover methods to controls for methods which currently only exist on cell classes. So, you should do if ([NSTextField instancesRespondToSelector:@selector(setBackgroundStyle:)]) [super setBackgroundStyle:backgroundStyle]; to be future-safe.) (尽管NSTextField当前未实现backgroundStyle属性,但将来可能会实现。Apple表示将为控件仅在单元格类中存在的方法添加Cover方法。因此, if ([NSTextField instancesRespondToSelector:@selector(setBackgroundStyle:)]) [super setBackgroundStyle:backgroundStyle];以确保将来安全。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM