Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScaledFont doesn't work with UI legibilityWeight in SwiftUI #5

Open
chessboy opened this issue Aug 6, 2022 · 3 comments
Open

ScaledFont doesn't work with UI legibilityWeight in SwiftUI #5

chessboy opened this issue Aug 6, 2022 · 3 comments
Labels

Comments

@chessboy
Copy link

chessboy commented Aug 6, 2022

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

Love this library!

@kharrison
Copy link
Owner

kharrison commented Aug 8, 2022

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 kharrison added the enhancement New feature or request label Aug 8, 2022
@chessboy
Copy link
Author

chessboy commented Aug 13, 2022

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!

@kharrison
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants