Home > database >  How can i change the properties for the text in SwiftUI?
How can i change the properties for the text in SwiftUI?

Time:10-12

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.

  • Related