backpack-ios icon indicating copy to clipboard operation
backpack-ios copied to clipboard

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

Open subeenpark-io opened this issue 1 year ago • 1 comments

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!

subeenpark-io avatar May 10 '24 10:05 subeenpark-io

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!

gert-janvercauteren avatar May 11 '24 03:05 gert-janvercauteren