2
votes

I know that we can set a colored title for a button using attributed strings like this:

let style = NSMutableParagraphStyle()
style.alignment = .Center
button.attributedTitle = NSAttributedString(string: "title",
             attributes: [ NSForegroundColorAttributeName : NSColor.redColor(),
                    NSParagraphStyleAttributeName : style])

In fact that's how I'm getting the red title to begin with, but it has no effect on the title color in the disabled mode (it is always gray); I guess in order to set that, I have to create an instance of the NSButton class and override some of its methods related to title color or override some of its properties to disable user interactions(when necessary) instead of disabling the button, but I don't know how.

There are some possible objective-C answers to this question here, here and here.

2
This is a desired behavior to let the user know that the button is disabled. I would not recommend changing the button behavior.Leo Dabus
don't disable it. Just change its appearance and functionalityLeo Dabus
@LeoDabus How can I make it unclickable? I mean when it is pressed its appearance changes(its color becomes dark gray) how can I stop that from happening without using the disabled mode?Shayan Shahsiah

2 Answers

0
votes

Do you need it to be disabled by the definition of it not doing anything when touched?

If so, just have a flag in the button clicked function. So then, the button color could be red (example) when disabled but you can click it but nothing happens. Then if it it's green (example) then it'll allow it to do something.

Your flag could be the color of the button checked through an if statement or set an actual variable.

0
votes

Create a NSButtonCell Subclass and assign it to the CELL (not the button) in IB.

Then implement

func drawTitle(_ title: NSAttributedString, 
 withFrame frame: NSRect, 
        in controlView: NSView) -> NSRect

and return different attributedString for isEnabled or !isEnabled.

You might also set the initial attributedTitle in

init (id)

EDIT: Only works if setWantsLayers is false