i want to change the properties for some text but i got this error " Cannot infer contextual base in reference to member 'applyLabelStyle'" .
This is how im trying to do it :
Text("Destinatar")
colorScheme == .light ? .applyLabelStyle() : .applyLabelStyleDark() // here i got the error
.applyLabelStyle() and .applyLabelStyleDark() are located in a text extension
func applyHeadingStyle() -> some View {
self.foregroundColor(Color(UIColor.colorGrayDark1))
.font(.system(size: 16))
}
func applyHeadingStyleDark() -> some View {
self.foregroundColor(.white)
.font(.system(size: 16))
}
How can i show different text based on light or dark theme ?
CodePudding user response:
You can't use a ternary operator in the way that you are doing in SwiftUI.
But... you could update your code to be like...
func applyHeadingStyle(colorScheme: ColorScheme) -> some View {
self.foregroundColor(colorScheme == .light ? Color(UIColor.colorGrayDark1) : .white)
.font(.system(size: 16))
}
Now in your SwiftUI you can do...
Text("Destinatar")
.applyHeadingStyle(colorScheme: colorScheme)
This should do what you want.