有没有办法在 swiftui 中更改 sf 符号图标的笔触/填充颜色?

Tay*_*man 25 swiftui sf-symbols

我正在寻找一种方法来更改 swiftui 中 sf 符号图标的笔触/填充颜色。

我已经尝试过,.background(Color.red)但这只是改变了整个图标的背景(没有改变应用于实际图标本身),正如暗示的那样,然后我也尝试.foregroundColor(Color.red)了对图标没有任何作用。

内容视图的内容如下

var body: some View {
Image(systemName: "person.circle").foregroundColor(.red)    
}

Run Code Online (Sandbox Code Playgroud)

小智 32

您可以使用以下命令更改 sf 符号图标的笔触和填充颜色 foregroundColor(_ color: Color?)

以下代码:

Image(systemName: "flame.fill").foregroundColor(.red)
Run Code Online (Sandbox Code Playgroud)
Image(systemName: "flame").foregroundColor(.red)
Run Code Online (Sandbox Code Playgroud)

应该产生这个:

填充和描边火焰 SF 符号图标

这是完整的 SwiftUI 查看代码

struct Icon : View {
    var body: some View {
        HStack{
            Image(systemName: "flame.fill")
            .foregroundColor(.red)
            Image(systemName: "flame")
            .foregroundColor(.red)
        }
        .padding()
    }
}
Run Code Online (Sandbox Code Playgroud)


Moj*_*ini 26

iOS 15

从 iOS 15 和 SFSymbols 3 开始,您可以使用前景样式修改器将不同的颜色层应用到单个符号:

Image(systemName: "person.circle")
    .resizable()
    .foregroundStyle(.red, .blue)
    .frame(width: 200, height: 200, alignment: .center)
Run Code Online (Sandbox Code Playgroud)

演示1


iOS 13 和 14

您可以将 aZStack与图标的不同部分一起使用,并对每个图层应用不同的修饰符,例如:

///  I've used `GeometryReader ` for setting the size of elements dependent on each other

GeometryReader { proxy in 
    ZStack {
        Image(systemName: "circle")
            .resizable()
            .foregroundColor(.blue)

        Image(systemName: "person.fill")
            .resizable()
            .foregroundColor(.red)
            .frame(
                   width: proxy.size.width * 0.55,
                   height: proxy.size.width * 0.55,
                   alignment: .center
            )
    }
}.frame(width: 200, height: 200, alignment: .center)
Run Code Online (Sandbox Code Playgroud)

演示2

请注意,新旧方法看起来略有不同,但感觉相同。(仔细看看头部的圆度)


paw*_*222 25

iOS 15+

在 iOS 15 中,我们可以使用foregroundStyle更多自定义 SF 符号:

Image(systemName: "cloud.sun.bolt")
    .foregroundStyle(.gray, .blue)
Run Code Online (Sandbox Code Playgroud)

在本次 WWDC 会议上查看更多信息:

在此输入图像描述


Fil*_*kel 2

就在这里:

var body: some View {
    Image(systemName: "person.circle").accentColor(.red)    
}
Run Code Online (Sandbox Code Playgroud)

  • 这在早期的测试版中有效。我认为目前正确的方法是使用 .foregroundColor(.red)。 (3认同)