Issue #847
We should use Dynamic Font Type as much as possible, as per Typography guide and https://www.iosfontsizes.com/
But in case we have to use a specific font, we can scale it with UIFontMetrics
import SwiftUI
import UIKit
extension Font {
static func system(
scaledSize size: CGFloat,
weight: Font.Weight = .regular,
design: Font.Design = .default
) -> Font {
Font.system(
size: UIFontMetrics.default.scaledValue(for: size),
weight: weight,
design: design
)
}
}
Then instead of
Text("Hello World")
.font(.system(size: 16))
we can use a scaled version
Text("Hello World")
.font(.system(scaledSize: 16))
For dynamically updating, we can use a ViewModifier
that listens to sizeCategory
struct ScaledFont: ViewModifier {
@Environment(\.sizeCategory) var sizeCategory
var size: CGFloat
func body(content: Content) -> some View {
let scaledSize = UIFontMetrics.default.scaledValue(for: size)
return content
.font(.system(size: scaledSize))
}
}