ScaledFont icon indicating copy to clipboard operation
ScaledFont copied to clipboard

ScaledFont doesn't work with UI `legibilityWeight` in SwiftUI

Open chessboy opened this issue 2 years ago • 3 comments

Are there any plans to support legibilityWeight in SwiftUI or UIAccessibility.isBoldTextEnabled in UIKit?

Love this library!

chessboy avatar Aug 06 '22 16:08 chessboy

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.

kharrison avatar Aug 08 '22 11:08 kharrison

I was thinking something like this:

Font Props

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!

chessboy avatar Aug 13 '22 05:08 chessboy

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.

kharrison avatar Aug 18 '22 08:08 kharrison