如何反转所选NSTableView行的文本颜色 [英] How to invert text colour of selected NSTableView row

查看:87
本文介绍了如何反转所选NSTableView行的文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSTableView,其中正在更改特定列中文本的颜色,但是当选择一行时,文本不会更改为更合适的颜色,以便于阅读。

I have an NSTableView in which I'm changing the colour of the text in a particular column, but when a row is selected, the text does not change to a more appropriate colour so that it's readable.

Finder的日期修改,大小和种类列具有灰色文本,并且当您选择文件/文件夹行时,灰色文本变为白色(以便在蓝色突出显示)。

Finder's Date Modified, Size, and Kind columns have grey text, and when you select a file/folder row, the grey text changes to white (so that it's readable within the blue highlight).

我无法在XCode上找到一个魔术复选框来默认启用此行为,所以有人知道我将如何实现相同的效果吗?

I can't find a magic checkbox on XCode to enable this behaviour by default, so does anybody know how I might achieve the same effect?

我在XCode 6.3中使用Swift。

I'm using Swift in XCode 6.3.

谢谢。

推荐答案

您没有说要为单元格使用哪种视图或视图层次结构。您也不会说出如何或在何处设置文本字段的颜色或具体设置为哪种颜色。

You don't say what view or view hierarchy you're using for your cells. You also don't say how or where you're setting the text fields' color or to what color, specifically.

选中某行时,该行会自动计算其 interiorBackgroundStyle 。如果它响应 -setBackgroundStyle:或为 backgroundStyle 。 > NSControl 带有一个对此作出响应的单元格。

When a row is selected, the row automatically computes its interiorBackgroundStyle. It also sets the backgroundStyle of the cell view if it responds to -setBackgroundStyle: or is an NSControl with a cell which responds to that.

如果您的单元格视图是 NSTableCellView ,它将背景样式转发到所有满足相同条件的子视图。如果您使用其他容器视图作为单元格视图,并且希望像这样转发背景样式,则必须在视图类中自己实现。

If your cell view is an instance of NSTableCellView, it forwards the background style to all of its subviews which meet the same criteria. If you use a different container view as your cell view and you want the background style forwarded along like this, you would have to implement that yourself in your view class.

NSTextField 的单元格( NSTextFieldCell )的单元格响应 -setBackgroundStyle:并通过上述机制自动设置了背景样式。如果文本字段单元格的 textColor 是标准控件颜色之一(例如 NSColor.controlTextColor()),它将自动将其文本颜色更改为白色。 code>),但如果您分配了非标准颜色,则不会这样做。因此,如果您要为文本设置特定的颜色,则有责任在背景样式更改时进行更改。

An NSTextField's cell (an NSTextFieldCell) responds to -setBackgroundStyle: and so has its background style set automatically by the above mechanisms. The text field cell will automatically change its text color to white if its textColor is one of the standard control colors (e.g. NSColor.controlTextColor()), but won't do so if you assign a non-standard color. So, if you're setting a specific color for your text, you are responsible for changing that when the background style changes.

您可以使用<$ c的子类$ c> NSTableCellView 并为 backgroundStyle 属性添加一个属性观察器( didSet )。可以根据设置的样式更改文本字段的 textColor 。例如,如果背景样式不是 .Dark ,则可以使用自定义颜色,也可以使用普通文本字段颜色 NSColor.controlTextColor()(如果是。是深色 c)(这样文本字段实际上将显示为白色)。

You can use a subclass of NSTableCellView and add a property observer (didSet) for the backgroundStyle property. That can change the text field's textColor depending on the style that was set. For example, you can use your custom color if the background style is not .Dark or use the normal text field color NSColor.controlTextColor() if it is .Dark (so that the text field will actually display it as white).

您也可以将 NSTextFieldCell 的子类用于文本字段,并执行相同的操作。或重写 drawInteriorWithFrame(_:inView:)来绘制不同的文本颜色,具体取决于背景样式。

You could also use a subclass of NSTextFieldCell for your text field and do the same sort of thing. Or override drawInteriorWithFrame(_:inView:) to draw with a different text color depending on the background style.

这篇关于如何反转所选NSTableView行的文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆