Search code examples
swiftswiftuiswift-playground

Turn all Text() on swift app to the same color in SwiftUI


I have used the Text("") object multiple times in my Swift Playground app but I want to change the color of all of them to a specific color (white) without changing each Text property one by one. Is there a way to do this?

Disclaimer: I am programming in a Swift Playground


Solution

  • You can create a custom ViewModifier

    struct MyTextModifier: ViewModifier {
        func body(content: Content) -> some View {
            content
                .foregroundColor(Color.white)
        }
    }
    

    Then you can apply it to Text where needed, and you only need to change it in the ViewModifier struct.

    struct ContentView: View {
        var body: some View {
            Text("Your Text")
                .modifier(MyTextModifier())
        }
    }