I am looking for a way to change the stroke / fill color of an SF Symbol icon in SwiftUI.
I have tried .background(Color.red)
but that just changes the background of the whole icon (no change is applied to the actual icon itself) as implied. I also tried .foregroundColor(Color.red)
which does nothing to the icon.
contents of content view are as follows:
var body: some View {
Image(systemName: "person.circle").foregroundColor(.red)
}
If you use SF Symbols in SwiftUI's Image view, you can get simple colors using the foregroundColor() attribute, or enable their multicolor variants by using . renderingMode(. original) .
For a full overview of the available SF Symbols that are available, including the newly added and multicolor symbols, download the SF Symbols 2 app from Apple's SF Symbols page. To use a multicolored symbol in your app, all you need to do is set the correct rendering mode for your image.
SwiftUI uses the systemName parameter for SF Symbol lookup. Keep in mind that you can use string interpolation to show an SF Symbol as the part of any text. Another SwiftUI view that plays well with SF Symbols is Label. The Label view contains both text and an image and shows them according to the current context.
Set Image As Button's Background Color. Besides setting the button image icon, you can also set an image as the swift button's background color. To do this, just create a UIColor object use a selected UIImage object, then set the UIColor object as the button's backgroundColor property value.
You can change the stroke and fill color of a sf symbol icon using foregroundColor(_ color: Color?)
The following code:
Image(systemName: "flame.fill").foregroundColor(.red)
Image(systemName: "flame").foregroundColor(.red)
Should produce this:
Here is the complete SwiftUI View Code
struct Icon : View {
var body: some View {
HStack{
Image(systemName: "flame.fill")
.foregroundColor(.red)
Image(systemName: "flame")
.foregroundColor(.red)
}
.padding()
}
}
from iOS 15 and with the SFSymbols 3, you can apply different layers of colors to a single symbol with the foreground style modifier:
Image(systemName: "person.circle")
.resizable()
.foregroundStyle(.red, .blue)
.frame(width: 200, height: 200, alignment: .center)
You can use a ZStack
with different parts of the icon and apply different modifiers to each layer like:
/// 💡 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)
Note that the old and new methods look slightly different but feel the same. (take a closer look at the roundness of the head)
In iOS 15 we can use foregroundStyle
to customise SF Symbols even more:
Image(systemName: "cloud.sun.bolt")
.foregroundStyle(.gray, .blue)
See more at this WWDC session:
Yes there is:
var body: some View {
Image(systemName: "person.circle").accentColor(.red)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With