SwiftUI - How to change the color of a custom SF symbol in a Text View?

Viewed 305

So, I have a Text in a SwiftUI view that has a custom SF symbol in it:

Text("This is some text with a   \(Image(uiImage: UIImage(named: "customSFSymbol")!))  symbol.")

The problem is the following:

I want to make all that view white, but when I add the .foregroundColor(Color.white) to the view, the SF Symbol stays in a black color.

You can see how it looks right now here

Thanks for your help in advance!

2 Answers

You can simply add .renderingMode(.template) to the Image

Text("This is some text with a   \(Image(uiImage: UIImage(named: "customSFSymbol")!).renderingMode(.template))  symbol.")

You can use like this

struct ContentView: View {
    var body: some View {
        Text("This is some text with a ").foregroundColor(Color.red) + imageText + Text("symbol.").foregroundColor(Color.red)
    }
    
    @ViewBuilder
    var imageText: Text {
        Text("\(Image(systemName: "square.and.pencil"))")
            .foregroundColor(Color.blue)
    }
}
blue symbol within red text
Related