shah.shah shah.shah - 1 year ago 209
Swift Question

Changing the color and font size of button titles in swift(for osx NOT ios)

I want to change the color of one of my button titles. First I tried some simple guesses:

Button.title.textColor = NSColor(/*something*/)
Button.Color = NSColor( /*something*/ )
Button.textColor = NSColor( /*something*/ )

But they didn't work. Then I found the following code on the internet:

Button.attributedTitle = NSAttributedString(string: /*something*/ , attributes: /*something*/ [NSForegroundColorAttributeName : NSColor( /*something*/ ), NSParagraphStyleAttributeName : NSMutableParagraphStyle() ])

But it's long and complicated and can't change the color without assigning additional characteristics(other than leaving them to the default values set in the interface builder); also, most importantly, when I use it, it changes the font size to a small font size(ignoring the font size I set in the interface builder) and I don't know how to change it back again.
Is there any better code that I can use? If not how can I assign a value for the font size in the code above?

Answer Source

Create an attributed string which includes both color and font size (more information: How do I make an attributed string using Swift? ) And then use


to set the title

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download