Created
October 27, 2023 11:27
-
-
Save shaps80/c43fa90964d6b21172a392f7a816f710 to your computer and use it in GitHub Desktop.
Automatically scales the font size in respect to dynamic type size changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public extension View { | |
func font<T: BinaryFloatingPoint>(_ scaled: Scaled<T>) -> some View { | |
modifier(ScaledFontModifier(metric: scaled.value)) | |
} | |
} | |
public struct Scaled<T: BinaryFloatingPoint> { | |
fileprivate let value: T | |
public static func scaled(size: T) -> Self { | |
.init(value: size) | |
} | |
} | |
private struct ScaledFontModifier<T: BinaryFloatingPoint>: ViewModifier { | |
@ScaledMetric fileprivate var metric: T | |
func body(content: Content) -> some View { | |
content.font(.system(size: .init(metric))) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: