Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Save the previous color of an NSTextFieldCell when selected
// Custom text colors don't automagically invert.
class TableViewTextFieldCell: NSTextFieldCell {
private var previousTextColor: NSColor?
override var backgroundStyle: NSView.BackgroundStyle {
get {
return super.backgroundStyle
}
set(newBackgroundStyle) {
// If we are going to light because we are selected, save off the old color so we can restore it
if self.backgroundStyle == .light && newBackgroundStyle == .dark {
previousTextColor = self.textColor
self.textColor = NSColor.white // or a named color?
} else if self.backgroundStyle == .dark && newBackgroundStyle == .light {
if previousTextColor != nil {
self.textColor = previousTextColor
previousTextColor = nil
}
}
super.backgroundStyle = newBackgroundStyle
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment