ButtonStyle font tracking or casting the configuration label?

Viewed 54

I'd like to set the text/label font properties in my ButtonStyle when using the simple Button constructor with a StringProtocol, not passing in a full view, but the configuration label in the makeBody function won't allow it. I get why - makeBody returns some View which doesn't have the tracking and textCase modifiers I'd like to use.

Is there a way to cast the configuration label to be a Text view or some way for me to put the additional font modifiers (tracking & textCase) directly into my ButtonStyle so I don't have to pass in a Text view with additional modifiers?

// The Text properties I'd like to modify which are not available
// on the configuration label, so I've added them to this Text extension
extension Text {
  func buttonText() -> some View {
    self.tracking(2.0).textCase(.uppercase)
  }
}

// What I would like...
Button("Some Text") {
  // Some action
}
  .buttonStyle(BrandedButton())

// What I currently have to do
Button(action: {}, label: {
  Text("Some Text")
    .buttonText()
})
  .buttonStyle(BrandedButton())

BrandedButton is my ButtonStyle. My obvious concern is that this duplication - having to add the .buttonText() modifier everywhere - is easily overlooked and can result in some mismatched styling of buttons. .font is a valid modifier for configuration.label, but the others are not.

0 Answers
Related