Your First iOS and SwiftUI App · Challenge: View Modifiers | raywenderlich.com


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.

Screenshot 2020-12-08 at 5.15.42 PM

This kind of inheritance is not working currently.