Skip to content

Instantly share code, notes, and snippets.

@mminer
Last active September 10, 2022 23:24
Show Gist options
  • Save mminer/134b3a0ed6be6b3eb3e305b63d96a126 to your computer and use it in GitHub Desktop.
Save mminer/134b3a0ed6be6b3eb3e305b63d96a126 to your computer and use it in GitHub Desktop.
NSButton extension that adds a method to change its text color.
import AppKit
extension NSButton {
func set(textColor color: NSColor) {
let newAttributedTitle = NSMutableAttributedString(attributedString: attributedTitle)
let range = NSRange(location: 0, length: attributedTitle.length)
newAttributedTitle.addAttributes([
.foregroundColor: color,
], range: range)
attributedTitle = newAttributedTitle
}
}
@pjkoning
Copy link

It works great, but not on macOS 10.15. Do you have any clue?

@Tohr01
Copy link

Tohr01 commented Sep 10, 2022

Hey @pjkoning.

You might want to try something like this:

let color: NSColor = NSColor.white
if #available(macOS 10.14, *) {
     self.contentTintColor = color
} else {
      // Fallback on earlier versions
      self.set(textColor: color)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment