Skip to content

Instantly share code, notes, and snippets.

@Frizlab
Created September 5, 2018 09:00
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 Frizlab/0f79fd7f1663e95455a24902c502805b to your computer and use it in GitHub Desktop.
Save Frizlab/0f79fd7f1663e95455a24902c502805b to your computer and use it in GitHub Desktop.
class HighlightColorTextFieldCell : NSTextFieldCell {
var hightlightColor: NSColor? {
didSet {
updateTextColor()
}
}
var nonHighlightedTextColor: NSColor? {
didSet {
updateTextColor()
}
}
override var backgroundStyle: NSView.BackgroundStyle {
didSet {
updateTextColor()
}
}
private func updateTextColor() {
switch backgroundStyle {
case .raised, .emphasized: textColor = hightlightColor
case .lowered, .normal: textColor = nonHighlightedTextColor
}
}
override func setUpFieldEditorAttributes(_ textObj: NSText) -> NSText {
let newTextObj = super.setUpFieldEditorAttributes(textObj)
newTextObj.textColor = NSColor.black
textColor = NSColor.black /* Not sure why the line above is not enough (and actually seems to do nothing...) */
return newTextObj
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment