Created
April 9, 2019 15:39
-
-
Save corbinstreehouse/2ac633e04204734948a4395e73c716e0 to your computer and use it in GitHub Desktop.
Save the previous color of an NSTextFieldCell when selected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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