Search code examples
iosswiftuidynamic-type-feature

How to scale system font in SwiftUI to support Dynamic Type?


In UIKit, I can change a UILabel's font size like this to support dynamic type using the system font:

UIFontMetrics.default.scaledFont(for: UIFont.systemFont(ofSize: 16))

Am I mistaken or is there no way to do such thing in SwiftUI? Can I only use the default font styles .title, etc. if I want to use the system font but also support Dynamic Type?

This solution from HackingWithSwift seems to only work with custom fonts, right?

Thanks in advance for any suggestions!


Solution

  • The following approach works (tested with Xcode 11.2.1 / iOS 13.2.2)

    var body: some View {
        Text("Hello, World!") // direct text .font
            .font(Font.system(size: UIFontMetrics.default.scaledValue(for: 16)))
    }
    

    as well as for view-based modifier

    VStack {
        Text("Hello, World!")
    }
    .font(Font.system(size: UIFontMetrics.default.scaledValue(for: 16)))