How do I set a weight to SF Symbols for iOS 13?
Asked Answered
T

8

115

I have this

Image(systemName: "arrow.right")

But how do I make it bold, semibold etc?

I am using the new SwiftUI.

Teenateenage answered 9/6, 2019 at 22:8 Comment(0)
E
180

When using the font modifier, set a weight to the font you're passing.

For example, if you want to use one of the default text styles (which I recommend, since they adapt to the user's Dynamic Type setting), you can do it like this:

Image(systemName: "arrow.right")
  .font(Font.title.weight(.ultraLight))

If you want to specify a font size, you can do it like this:

Image(systemName: "arrow.right")
  .font(Font.system(size: 60, weight: .ultraLight))
Extent answered 9/6, 2019 at 22:19 Comment(3)
I tried this, but this seems to change the size and not the weight (bold, light, semibold ect). Unless i misunderstood?Teenateenage
My bad, it was I who misunderstood. I've updated my answer.Extent
For default size this would be Font.body.weight(.ultraLight) (for example inside navigationBarItems) (all font weights)Achitophel
E
84

For UIKit, symbols can be configured as follows:

UIImage(systemName: "arrow.right",
        withConfiguration: UIImage.SymbolConfiguration(pointSize: 16, weight: .bold))
Endocentric answered 2/9, 2019 at 17:51 Comment(0)
W
29

SwiftUI 1.0

I just wanted to also mention how to change the weight along with a custom font size.

HStack(spacing: 40) {
    Image(systemName: "moon.zzz")
        .font(Font.system(size: 60, weight: .ultraLight))
    Image(systemName: "moon.zzz")
        .font(Font.system(size: 60, weight: .light))
    Image(systemName: "moon.zzz")
        .font(Font.system(size: 60, weight: .regular))
    Image(systemName: "moon.zzz")
        .font(Font.system(size: 60, weight: .bold))
}

Example of Font Size and weight

Whittaker answered 5/9, 2019 at 16:27 Comment(1)
This should be the accepted answer I think as the question is dealing with SwiftUI and it sets the .system font not the .title ← which could interfere with other styling.Ardelia
F
28

UIKit -- Swift 5 -- Xcode 11

If you only want to set the weight (so you don't mess up auto icon sizing), do this:

let configuration = UIImage.SymbolConfiguration(weight: .semibold)
UIImage(systemName: "trash", withConfiguration: configuration)
Fraction answered 10/7, 2020 at 21:38 Comment(0)
B
27

You can also wrap the Image with Text. With that you can use and assign the fontWeight() to Text:

Text(Image(systemName: "xmark"))
    .fontWeight(.semibold)
Bowstring answered 7/11, 2020 at 11:53 Comment(3)
This is the best solution for SwiftUI as the font size doesn't have to be set.Centrality
It's a nice solution but available from iOS 16 onlyAllare
It has been working for me since 2020 at least from iOS 14 up.Bowstring
H
15

UIKit SWIFT 5.x

To set their attributes: create a configuration then pass it in as a parameter:

let imageConfig = UIImage.SymbolConfiguration(pointSize: 22, weight: .black, scale: .large)
let image = UIImage(systemName: "delete.right", withConfiguration: imageConfig)
Huddleston answered 28/1, 2020 at 20:15 Comment(0)
M
10

For iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, you can use fontWeight() directly on the Image().

Image(systemName: "chevron.right")
    .fontWeight(.semibold)
Marcionism answered 19/11, 2022 at 8:3 Comment(1)
Thank you! This is the best answer for iOS 16.0+Fabrin
S
1

Xcode 13.4, Swift 5.X

import UIKit
import SwiftUI


public extension Image {
    
    @available(iOS 13.0, *)
    static func buildSystemImage(named systemName: String, weightConfiguration: UIImage.SymbolWeight) -> Image? {
        guard let imageConfig = UIImage(systemName: systemName, withConfiguration: UIImage.SymbolConfiguration(weight: weightConfiguration)) else { return nil }
        return Image(uiImage: imageConfig)
    }
}

Usage:

if let systemImage = Image.buildSystemImage(named: "arrow.left", weightConfiguration: .semibold) {
    // Your code.                
}
Settler answered 30/8, 2022 at 20:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.