0

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. 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). 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?

enter image description here

4

1 回答 1

4

没有自动的方法可以得到你想要的。您使用黑色标签看到的自动行为是由NSTextFieldCell(或其超类之一,如NSCell)实现的。它由单元格的backgroundStyleto设置触发NSBackgroundStyleDark。单元格的backgroundStyle设置是NSTableCellView在其自己backgroundStyle的设置时设置的。NSTableCellView's由其更改时backgroundStyle设置,这发生在设置其他属性(如和)时。NSTableRowViewinteriorBackgroundStyleselectedemphasized

textColor无论如何,如果单元格是[NSColor controlTextColor]或具有等效的 RGB 值,则该单元格只会自动更改其用于绘制的颜色。因此,它不适用于您的灰色标签。

您可以实现 or 的自定义子NSTableCellViewNSTextField。您的类将实现 (override, for a subclass of NSTableCellView) -setBackgroundStyle:。在您的方法中,您可以检查正在设置的样式并更改文本字段的textColor. 如果是覆盖,请调用 super。(虽然NSTextField目前没有实现backgroundStyle属性,但将来可能会实现。Apple 已经表示他们将为当前仅存在于单元格类中的方法的控件添加覆盖方法。因此,您应该这样做if ([NSTextField instancesRespondToSelector:@selector(setBackgroundStyle:)]) [super setBackgroundStyle:backgroundStyle];以确保未来安全。)

于 2015-06-27T18:06:14.003 回答