簡體   English   中英

SwiftUI - 如何在文本視圖中更改自定義 SF 符號的顏色?

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

所以,我在 SwiftUI 視圖中有一個文本,其中有一個自定義 SF 符號:

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

問題如下:

我想讓所有視圖都變成白色,但是當我將.foregroundColor(Color.white)添加到視圖時,SF 符號保持黑色。

你可以在這里看到它現在的樣子

提前感謝您的幫助!

您可以簡單地將.renderingMode(.template)添加到Image

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

你可以這樣使用

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