【问题标题】:SwiftUI - How to change the color of a custom SF symbol in a Text View?SwiftUI - 如何在文本视图中更改自定义 SF 符号的颜色?
【发布时间】:2021-05-13 08:38:08
【问题描述】:

所以,我在 SwiftUI 视图中有一个文本,其中有一个自定义 SF 符号:

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

问题如下:

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

你可以看到它现在的样子here

提前感谢您的帮助!

【问题讨论】:

  • 如果你使用.withTintColor(_:renderingMode:)会发生什么,像这样:.withTintColor(UIColor.white, renderingMode: .alwaysOriginal)
  • 您可以将.renderingMode(.template)添加到Image
  • =>Text("This is some text with a \(Image(uiImage: UIImage(named: "customSFSymbol")!).renderingMode(.template)) symbol.")
  • 非常感谢@CédricBhr!效果很好!

标签: ios swift swiftui swift5 ipados


【解决方案1】:

您可以简单地将.renderingMode(.template) 添加到Image

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

【讨论】:

    【解决方案2】:

    你可以这样使用

    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)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-04
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 2019-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-02
      相关资源
      最近更新 更多