简体   繁体   English

SwiftUI ViewModifier - 添加字距调整

[英]SwiftUI ViewModifier - add kerning

Is there a way to build a view modifier that applies custom font and fontSize, as the below working example, and have in the same modifier the possibility to add kerning as well?有没有办法构建一个应用自定义字体和 fontSize 的视图修饰符,如下面的工作示例,并且在同一个修饰符中也可以添加字距调整?

struct labelTextModifier: ViewModifier {
    var fontSize: CGFloat

    func body(content: Content) -> some View {
        content
            .font(.custom(Constants.defaultLabelFontSFProDisplayThin, size: fontSize))
    }
}

extension View {   
    func applyLabelFont(size: CGFloat) -> some View {
        return self.modifier(labelTextModifier(fontSize: size))
    }
}

The above works well, however i cannot figure it out how to add kerning to the modifier as well上面的效果很好,但是我不知道如何向修饰符添加字距调整

tried试过了

content
    .kerning(4)

, but did not work. ,但没有奏效。

Suggestions?建议?

Alternate is to use Text-only modifier, like另一种方法是使用纯文本修饰符,例如

extension Text {   
    func applyLabelFont(size: CGFloat, kerning: CGFloat = 4) -> Text {
        self
          .font(.custom(Constants.defaultLabelFontSFProDisplayThin, size: size))
          .kerning(kerning)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM