Skip to content

Instantly share code, notes, and snippets.

@zhaorui
Created February 8, 2017 15:59
Show Gist options
  • Save zhaorui/112ff42f52eb430ff7252a49b26343f9 to your computer and use it in GitHub Desktop.
Save zhaorui/112ff42f52eb430ff7252a49b26343f9 to your computer and use it in GitHub Desktop.
NSButton extension to set title color
extension NSButton {
var titleTextColor : NSColor {
get {
let attrTitle = self.attributedTitle
return attrTitle.attribute(NSForegroundColorAttributeName, at: 0, effectiveRange: nil) as! NSColor
}
set(newColor) {
let attrTitle = NSMutableAttributedString(attributedString: self.attributedTitle)
let titleRange = NSMakeRange(0, self.title.characters.count)
attrTitle.addAttributes([NSForegroundColorAttributeName: newColor], range: titleRange)
self.attributedTitle = attrTitle
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment