Skip to main content

How to get custom colors and transparency with SF Symbols

About 3 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to get custom colors and transparency with SF Symbols 관련

SwiftUI by Example

Back to Home

How to get custom colors and transparency with SF Symbols | SwiftUI by Example

How to get custom colors and transparency with SF Symbols

Updated for Xcode 15

New in iOS 15

If you use SF Symbols in SwiftUI's Image view, you can get simple colors using the foregroundStyle() modifier, or enable their multicolor variants by using .renderingMode(.original). However, for extra flexibility over individual parts of the image, you can use the hierarchical variant or provide a completely custom palette.

Hierarchical rendering uses opacity to create variations in shade on-screen. For example, this will draw the image in transparency to provide extra depth and clarity:

Image(systemName: "theatermasks")
    .symbolRenderingMode(.hierarchical)
    .font(.system(size: 144))

Download this as an Xcode projectopen in new window

A symbol showing a smiling mask in the foreground and a fainter sad mask in background.
A symbol showing a smiling mask in the foreground and a fainter sad mask in background.

Hierarchical rendering works in combination with foreground color, so you can specify both if you need to:

Image(systemName: "theatermasks")
    .symbolRenderingMode(.hierarchical)
    .foregroundStyle(.blue)
    .font(.system(size: 144))

Download this as an Xcode projectopen in new window

A symbol showing a smiling blue mask in the foreground and a fainter sad blue mask in background.
A symbol showing a smiling blue mask in the foreground and a fainter sad blue mask in background.

For even more power, you can use the .palette variant to get complete control over the colors in the image. So, we could render the SharePlay icon both blue and black at the same time, like this:

Image(systemName: "shareplay")
    .symbolRenderingMode(.palette)
    .foregroundStyle(.blue, .black)
    .font(.system(size: 144))

Download this as an Xcode projectopen in new window

The Apple Shareplay symbol showing a blue person icon in front of two black arcs.
The Apple Shareplay symbol showing a blue person icon in front of two black arcs.

How those colors are applied depends on each individual symbol – sometimes symbols are defined with two layers and sometimes three, and you'll need to explore them individually to see how they break down.

For symbols that contain three variants, just add an extra color:

Image(systemName: "person.3.sequence.fill")
    .symbolRenderingMode(.palette)
    .foregroundStyle(.blue, .green, .red)
    .font(.system(size: 144))

Download this as an Xcode projectopen in new window

Three slightly overlapping person icons, in blue, green, and red from left to right.
Three slightly overlapping person icons, in blue, green, and red from left to right.

This even works with complex foreground styles, such as providing one gradient for each person in the icon:

Image(systemName: "person.3.sequence.fill")
    .symbolRenderingMode(.palette)
    .foregroundStyle(
        .linearGradient(colors: [.red, .black], startPoint: .top, endPoint: .bottomTrailing),
        .linearGradient(colors: [.green, .black], startPoint: .top, endPoint: .bottomTrailing),
        .linearGradient(colors: [.blue, .black], startPoint: .top, endPoint: .bottomTrailing)
    )
    .font(.system(size: 144))

Download this as an Xcode projectopen in new window

Three slightly overlapping person icons, in blue, green, and red from left to right. Each icon's color transitions to black as it approaches the bottom right corner.
Three slightly overlapping person icons, in blue, green, and red from left to right. Each icon's color transitions to black as it approaches the bottom right corner.
Similar solutions…
How to animate SF Symbols | SwiftUI by Example

How to animate SF Symbols
How to render images using SF Symbols | SwiftUI by Example

How to render images using SF Symbols
Polishing designs with fonts and colors | SwiftUI by Example

Polishing designs with fonts and colors
How to load custom colors from an asset catalog | SwiftUI by Example

How to load custom colors from an asset catalog
How to style text views with fonts, colors, line spacing, and more | SwiftUI by Example

How to style text views with fonts, colors, line spacing, and more

이찬희 (MarkiiimarK)
Never Stop Learning.