Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the stroke/fill color of SF Symbol icon in SwiftUI?

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)    
}
like image 590
Taylor Miller-Klugman Avatar asked Jun 21 '19 19:06

Taylor Miller-Klugman


People also ask

How do I change the SF color in SwiftUI?

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

How do you use multicolor SF Symbols?

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.

How do I use SF Symbols in SwiftUI?

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.

How do I change the color of a button image in Swift?

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.


4 Answers

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:

Filled and Stroked Flame SF Symbol Icons

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()
    }
}
like image 82
Noemi Quezada Avatar answered Oct 16 '22 10:10

Noemi Quezada


iOS 15

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)

Demo 1


iOS 13 and 14

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)

Demo 2

Note that the old and new methods look slightly different but feel the same. (take a closer look at the roundness of the head)

like image 22
Mojtaba Hosseini Avatar answered Oct 16 '22 11:10

Mojtaba Hosseini


iOS 15+

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:

enter image description here

like image 5
pawello2222 Avatar answered Oct 16 '22 11:10

pawello2222


Yes there is:

var body: some View {
    Image(systemName: "person.circle").accentColor(.red)    
}
like image 2
Filip Sakel Avatar answered Oct 16 '22 10:10

Filip Sakel