Search code examples
xcodeswift3nscolorwell

How to change the action of NSColorWell?


I can use NSColorWell as button to change the color of selected text. Since NSColorWell is object of an NSControl it has target and action. I guess, the action is implementing the code to change the color of the selected text in NSTextView. Where can I find this code for NSColorWell action? I would like to change it in away that I can use NSColorWell to change the background of the selected text, and ultimately to have in ToolBar two NSColorWell buttons: one to change text's foreground color and second one for text's background color.


Solution

  • NSColorWell is just a rectangular control to change a color.

    • You can either create an IBAction and connect it to the action of the color well in the Connections Inspector (⌥⌘6) of Interface Builder

      @IBAction func changeColor(_ sender : NSColorWell)
      {
         let color = sender.color
         // do something with the color
      }
      
    • Or bind the value in Bindings Inspector (⌥⌘7) of Interface Builder to a dynamic property, this example will set the color well to a default value of green.

      dynamic var color : NSColor = .green {
         didSet {
           // do something with the color
         }
      }