How to use UIButtonConfiguration to set attributedTitle based on control state?

I use for (NSNumber *controlState in AllControlStates()) { [uiButton setAttributedTitle:attributedStrings[controlState] forState:controlState.unsignedIntegerValue]; } to set different title colors based on different controlState now.

How to use UIButtonConfiguration to set attributedTitle based on control state?

I am deprecating contentEdgeInsets for my button now, I have to set all the other properties on configuration to maintain the current look of my button.

You can set a configurationUpdateHandler which gives you the opportunity to update the configuration based on button state as you see fit. For example:

button.configurationUpdateHandler = { button in 
  var config = button.configuration
  config.title = button.isEnabled ? "Enabled" : "Disabled"
  button.configuration = config
}

Will produce a button whose title is "Enabled" when the button is enabled and "Disabled" when the button is disabled. You can also use setNeedsUpdateConfiguration to cause this handler to be called on the next update cycle, allowing you to incorporate any state you want into the update handler.

How to use UIButtonConfiguration to set attributedTitle based on control state?
 
 
Q