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

ScaledFontModifier lineHeight bug modifier seems like it's not gonna work as intended #1960

Open
subeenpark-io opened this issue May 10, 2024 · 1 comment

Comments

@subeenpark-io
Copy link

subeenpark-io commented May 10, 2024

It seems that the ScaledFont modifier isn't functioning as intended because the line spacing is set to 0 (source).

private struct ScaledFont: ViewModifier {
    var style: BPKFontStyle
    
    func body(content: Content) -> some View {
        return content.font(style.font)
            .lineSpacing(style.lineHeight - style.lineHeight) // currently 0! Will not work
    }
}

As you can see, the height of the textarea is set to threeLineHeight, but there's extra margin left even when you write three lines.

The corrected code should look like this. Both lineSpacing(1) and padding(2) should be modified to achieve the same visual result as the lineHeight from UIKit. (+you'll need to transform the metrics (fontSize, padding) relative to the current context's sizeCategory if you want to fully support dynamic type size)

private struct ScaledFont: ViewModifier {
    var style: BPKFontStyle
    
    func body(content: Content) -> some View {
        return content.font(style.font)
            .lineSpacing(lineSpacing) ----- (1)
            .padding(.vertical, lineSpacing / 2) ----- (2)
    }
    
    var lineSpacing: CGFloat { style.lineHeight - style.font.lineHeight }  ------ (1) 
}

However, since SwiftUI's Font doesn't provide a lineHeight property like UIFont did, you might need to revert the type of BPKFontStyle.font from Font to UIFont as it was before (source). If you're okay with this change, I can contribute, so please let me know!

@gert-janvercauteren
Copy link
Contributor

Hi @subeenpark-io,
Thanks for reaching out on this issue, we're more than happy for you to contribute a fix.

Looking forward to the fix and more than happy to support if you run into any issues!

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

No branches or pull requests

2 participants