This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4919757-your-first-ios-and-swiftui-app/lessons/42
This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4919757-your-first-ios-and-swiftui-app/lessons/42
View modifiers DRY code alternative(?)
I made the ValueStyle struct as follows
struct ValueStyle: ViewModifier {
func body(content: Content) -> some View {
return content
.foregroundColor(.yellow)
.font(Font.custom("Arial Rounded MT Bold", size: 24))
}
}
I left the .shadow
function out and I applied the following modifiers on the labels’ values:
Text("\(self.target)").modifier(ValueStyle()).modifier(LabelStyle())
I’m not sure If I’m correct but it seems like the second modifier (LabelStyle()) applies only the shadow on ValueStyle.
Is this true? Initially I wrote it like this:
Text("\(self.target)").modifier(LabelStyle()).modifier(ValueStyle())
because I thought that there was some kind of override going on like in CSS rules.
I expected that the LabelStyle’s color and font to get override by the ValueStyle’s and the shadow to remain as is. That wasn’t the case.
This kind of inheritance is not working currently.