问题
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 adynamic
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