NSTableCellView 中的 NSTextField
我有一个基于视图的 NSTableView 和一个自定义 NSTableCellView。这个自定义的 NSTableCellView 有几个标签(NSTextField)。 NSTableCellView 的整个 UI 都是用 IB 构建的。
NSTableCellView可以处于正常状态和选中状态。在正常状态下,所有文本标签应为黑色,在选定状态下,它们应为白色。
我该如何处理这个问题?
I have a view based NSTableView with a custom NSTableCellView. This custom NSTableCellView has several labels (NSTextField). The whole UI of the NSTableCellView is built in IB.
The NSTableCellView can be in a normal state and in a selected state. In the normal state all text labels should be black, in the selected state they should be white.
How can I manage this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
重写 NSTableCellView 上的 setBackgroundStyle: 来了解背景何时发生变化,这会影响您应该在单元格中使用的文本颜色。
例如:
在源列表表视图中,单元格视图的背景样式设置为“浅色”,其文本字段的背景样式也是如此,但是文本字段还在其文本下方绘制阴影,并且尚未找到确切的控制/确定应该它的原因发生。
Override setBackgroundStyle: on the NSTableCellView to know when the background changes which is what affects what text color you should use in your cell.
For instance:
In source list table views the cell view's background style is set to Light, as is its textField's backgroundStyle, however the textField also draws a shadow under its text and haven't yet found exactly what is controlling that / determining that should it happen.
完成此操作的最简单方法可能是子类化 NSTextField 并重写子类中的 drawRect: 方法。在那里,您可以使用以下代码确定当前是否选择了包含 NSTextField 实例的 NSTableCellView 实例(我将其与 NSOutlineView 一起使用,但它也应该与 NSTableView 一起使用):
然后像这样绘制视图:
Probably the easiest way to accomplish this would be to subclass NSTextField and to override the drawRect: method in your subclass. There you can determine whether the NSTableCellView instance containing your NSTextField instances is currently selected by using this code (which I use with a NSOutlineView, but it should also work with NSTableView):
Then draw the view like this:
无论表视图具有什么样式,这都有效:
This works no matter what style the table view has:
斯威夫特4
Swift 4