简体   繁体   English

SwiftUI - 如何在文本视图中更改自定义 SF 符号的颜色?

[英]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:所以,我在 SwiftUI 视图中有一个文本,其中有一个自定义 SF 符号:

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.我想让所有视图都变成白色,但是当我将.foregroundColor(Color.white)添加到视图时,SF 符号保持黑色。

You can see how it looks right now here你可以在这里看到它现在的样子

Thanks for your help in advance!提前感谢您的帮助!

You can simply add .renderingMode(.template) to the Image您可以简单地将.renderingMode(.template)添加到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)
    }
}
红色文本中的蓝色符号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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