ScaledFont doesn't work with UI `legibilityWeight` in SwiftUI
Are there any plans to support legibilityWeight in SwiftUI or UIAccessibility.isBoldTextEnabled in UIKit?
Love this library!
I have no immediate plans but I'm not against the idea if it's feasible. Have you given any thought as to how it would work?
Adding extra keys to specify a bold text variant is easy enough but I'm not sure how the auto updating would work if the user changes the setting? Does SwiftUI regenerate the views when the setting changes? Would need some investigating to see what is possible.
I was thinking something like this:
And font(forTextStyle) could be something like this:
internal func font(forTextStyle textStyle: Font.TextStyle, legibilityWeight: LegibilityWeight? = .regular) -> Font {
guard let styleKey = StyleKey(textStyle),
let fontDescription = styleDictionary?[styleKey.rawValue]
else {
return Font.system(textStyle)
}
let fontName = legibilityWeight == .bold ? fontDescription.legibilityFontName : fontDescription.fontName
return Font.custom(fontName, size: fontDescription.fontSize, relativeTo: textStyle)
}
Then in the view you'd have:
@Environment(\.legibilityWeight) var legibilityWeight
Text("Hello, World!")
.scaledFont(.largeTitle, legibilityWeight: legibilityWeight)
And yes, you get a redraw when legibilityWeight changes. Not the cleanest solution, admittedly, but thanks for looking into it!
I'll look into it some more. Finding a way that avoids having to add the parameter to every text view would be nice. I also need to think about how it would work for UIKit without needing to register every view controller for the notification.