Mac OSX NSButton Swift Header Color

I would like to know how to change the header color in NSButton in swift, I saw a lot of examples in objective-c, but I think the quick implementations are different, can anyone provide me an example?

+7
swift xcode6 macos nsbutton
source share
3 answers

try this in viewDidLoad or somewhere.

In Swift 3:

  let pstyle = NSMutableParagraphStyle() pstyle.alignment = .center button.attributedTitle = NSAttributedString(string: "Title", attributes: [ NSForegroundColorAttributeName : NSColor.red, NSParagraphStyleAttributeName : pstyle ]) 
+19
source share

Swift 4

I added this as an additional answer, since it only modifies the requested attribute without overwriting or adding additional ones.

 if let mutableAttributedTitle = button.attributedTitle.mutableCopy() as? NSMutableAttributedString { mutableAttributedTitle.addAttribute(.foregroundColor, value: NSColor.white, range: NSRange(location: 0, length: mutableAttributedTitle.length)) button.attributedTitle = mutableAttributedTitle } 
+1
source share

In Swift4

 button.attributedTitle = NSMutableAttributedString(string: "Hello World", attributes: [NSAttributedStringKey.foregroundColor: NSColor.white, NSAttributedStringKey.paragraphStyle: style, NSAttributedStringKey.font: NSFont.systemFont(ofSize: 18)]) 
0
source share

All Articles