我正在寻找一种在SwiftUI中更改SF Symbol图标的描边/填充颜色的方法。
我尝试过.background(Color.red)
,但那只会改变整个图标的背景(实际图标本身没有改变),正如其含义。我还尝试了.foregroundColor(Color.red)
,但对图标没有影响。
内容视图的内容如下:
var body: some View {
Image(systemName: "person.circle").foregroundColor(.red)
}
我正在寻找一种在SwiftUI中更改SF Symbol图标的描边/填充颜色的方法。
我尝试过.background(Color.red)
,但那只会改变整个图标的背景(实际图标本身没有改变),正如其含义。我还尝试了.foregroundColor(Color.red)
,但对图标没有影响。
内容视图的内容如下:
var body: some View {
Image(systemName: "person.circle").foregroundColor(.red)
}
您可以使用foregroundColor(_ color: Color?)
更改 SF 符号图标的描边和填充颜色。
下面的代码:
Image(systemName: "flame.fill").foregroundColor(.red)
Image(systemName: "flame").foregroundColor(.red)
应产生此结果:
这是完整的SwiftUI视图代码
struct Icon : View {
var body: some View {
HStack{
Image(systemName: "flame.fill")
.foregroundColor(.red)
Image(systemName: "flame")
.foregroundColor(.red)
}
.padding()
}
}
foregroundColor
对我很有帮助。你可能会认为 accentColor
更能描述这个功能,但那个不起作用。 - RamonImage(uiImage: UiImage("flame.fill"))
,则foregroundColor
对图像没有影响。 - Thomas.fill
或.outline
变体现在自动选择,具体取决于包含视图。例如,在Mac上的TabView
中。Mac和iOS选项卡视图不会同时使用.fill
。 - Pranav KasettiImage(uiImage: UiImage("flame.fill")).renderingMode(.template).foregroundColor(Color(.red))
。我仍然有一些通过PaintCode生成的编程图标返回为UIImages。 - Kpalser自 iOS 15 开始,使用 SFSymbols 3,您可以使用前景样式修饰符将不同层次的颜色应用于单个符号:
Image(systemName: "person.circle")
.resizable()
.foregroundStyle(.red, .blue)
.frame(width: 200, height: 200, alignment: .center)
您可以使用ZStack
来制作图标的不同部分,并对每一层应用不同的修饰符,例如:
/// 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)
请注意,旧方法和新方法看起来略有不同,但感觉相同。(仔细观察头的圆度)
.resizable
与systemImage
时,因为它会被转换成字体之外的其他东西。 - JAHelia在iOS 15中,我们可以使用foregroundStyle
更加自定义SF Symbols:
Image(systemName: "cloud.sun.bolt")
.foregroundStyle(.gray, .blue)
请参阅这个WWDC会话了解更多:
我发现在iOS14中一个不错的技巧是使用填充和非填充版本一起使用,以获取两种颜色,无论是14还是15:
ZStack {
Image(systemName: "person.circle")
.foregroundColor(.white)
Image(systemName: "person.circle.fill")
.foregroundColor(.yellow)
}
var body: some View {
Image(systemName: "person.circle").accentColor(.red)
}
SwiftUI现在允许您为许多SF Symbols的部分(图层)着色。一些符号有许多层。
这将使符号前景变为蓝色:
Image(systemName: "person.circle").foregroundStyle(.blue)
Image(systemName: "person.circle").foregroundStyle(.blue, .gray)
这将以红色绘制图标的背景和蓝色绘制线条:
Image(systemName: "person.circle")
.foregroundColor(Color.red)
.background(Color.blue)
.frame(width: size, height: self, alignment: .center)
.clipShape(Circle())
如果没有clipShape,圆形背后的矩形会有蓝色的角落。
只需更改色调颜色即可。这对我有效。
Image(systemName: "person.circle").foregroundColor(.red)
为我绘制了一个红色的人物圆圈。它为你绘制了什么? - rob mayoffContentView
以演示问题。然后将ContentView
的定义复制到您的问题中。 - rob mayoffImage(systemName: "person.circle").foregroundColor(.red)
可以正常工作,但我想像Image("clock").foregroundColor(.blue)
那样给自定义的标志上色,但它不起作用。 - Farras Doko