How to use SF Rounded in UIKit
Asked Answered
P

3

13

I have got a label and I spent quite a lot time trying to change the font of my UILabel to SF Rounded Bold.

Things like titleLabel.font = UIFont(name: "SFRounded-Bold", size: 34.0) or titleLabel.font = UIFont(name: "SanFranciscoRounded-Bold ", size: 34.0) don't work.

Is it even possible to use SF Rounded in UIKit? It is not listed in fonts list in scene editor and no one ever asked how to use it but in SwiftUI I can use SF Rounded without any problem.

Pylorus answered 30/1, 2020 at 8:8 Comment(1)
#56550564 There's answer from Pomme2Poule and Dan2899 that answers your question for UIKitDomiciliary
R
23

Swift 5, iOS 13+

Here's an extension version of the other post's answer

import UIKit

extension UIFont {
    class func rounded(ofSize size: CGFloat, weight: UIFont.Weight) -> UIFont {
        let systemFont = UIFont.systemFont(ofSize: size, weight: weight)
        let font: UIFont
        
        if let descriptor = systemFont.fontDescriptor.withDesign(.rounded) {
            font = UIFont(descriptor: descriptor, size: size)
        } else {
            font = systemFont
        }
        return font
    }
}

To use it:

let label = UILabel()
label.text = "Hello world!"
label.font = .rounded(ofSize: 16, weight: .regular)
Roemer answered 4/8, 2020 at 13:23 Comment(0)
S
9

More concise version of Kevin's answer:

import UIKit

extension UIFont {
    class func rounded(ofSize size: CGFloat, weight: UIFont.Weight) -> UIFont {
        let systemFont = UIFont.systemFont(ofSize: size, weight: weight)

        guard #available(iOS 13.0, *), let descriptor = systemFont.fontDescriptor.withDesign(.rounded) else { return systemFont }
        return UIFont(descriptor: descriptor, size: size)
    }
}

Usage:

let label = UILabel()
label.font = .rounded(ofSize: 16, weight: .regular)
Simdars answered 9/12, 2020 at 13:16 Comment(0)
D
3

Expanding on Artem's answer:

extension UIFont {
    func rounded() -> UIFont {
        guard let descriptor = fontDescriptor.withDesign(.rounded) else {
            return self
        }

        return UIFont(descriptor: descriptor, size: pointSize)
    }
}

Now you can add .rounded() to any font to get its rounded variant if it exists.

Dorren answered 28/4, 2021 at 17:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.