Ask Your Question
0

In SwiftUI, how can all Text() be made the same color throughout the app?

asked 2021-08-31 11:00:00 +0000

huitzilopochtli gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-10-07 07:00:00 +0000

devzero gravatar image

One way to achieve this is to define a custom style for Text and apply it to all instances of Text in the app. Here's an example:

  1. Declare a custom Text style in the app:

    extension Text {
       func myTextStyle() -> Text {
           self
               .foregroundColor(.black) // set the text color here
       }
    }
    
  2. Apply the custom style to all instances of Text in the app:

    struct ContentView: View {
       var body: some View {
           VStack {
               Text("Hello, world!")
                   .myTextStyle() // apply the custom style here
               Text("Another Text")
                   .myTextStyle() // apply the custom style here
               // more Text instances...
           }
       }
    }
    

By using this approach, any modifications to the custom style will be reflected throughout the app.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-08-31 11:00:00 +0000

Seen: 8 times

Last updated: Oct 07 '22