Skip to content

Instantly share code, notes, and snippets.

@corbinstreehouse
Created April 9, 2019 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corbinstreehouse/2ac633e04204734948a4395e73c716e0 to your computer and use it in GitHub Desktop.
Save corbinstreehouse/2ac633e04204734948a4395e73c716e0 to your computer and use it in GitHub Desktop.
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