简体   繁体   中英

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

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!

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)
    }
}
红色文本中的蓝色符号

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM