Search code examples
swiftmacoscocoacocoa-bindings

"disabled" style on cocoa text labels using bindings


I'm using Cocoa bindings (in Swift) to implement a preferences pane. Some of the settings only apply when other settings are turned on. This is mostly on check boxes, where its easy to use.

For instance, you can only "add a default name" when "add a default number" is turned on. This is easy to do by binding the enabled state on one check box to the same setting as the value in the other. Presto.

Now some of the prefs are not check boxes, but combos or text fields. These are labeled using NSTextFields, which also have an enabled binding. However, when these are disabled, the display does not change. I'd really like the text to become "disabled style" too, like it does on the checkbox.

So, does anyone know of a way to make this happen? There is a Text Color binding, but I'm not sure how I would use that in this case, at least without writing separate methods for every such example in the app.


Solution

  • You can create a value transformer class (subclass of NSValueTransformer) which transforms a boolean to a color. You can then apply that value transformer to a binding of a label's text color binding to a boolean property (presumably the same one that you're using to disable related controls).