How to change the action of NSColorWell?

情到浓时终转凉″ 提交于 2019-12-25 18:02:21

问题


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.


回答1:


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
       }
    }
    


来源:https://stackoverflow.com/questions/40616181/how-to-change-the-action-of-nscolorwell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!