如何在swiftUI中定义恒定的swiftUI类型字体?
我可以用以下代码在UIKIt中定义一个常量字体:如何在swiftUI中定义恒定的swiftUI类型字体?,swiftui,Swiftui,我可以用以下代码在UIKIt中定义一个常量字体: let appFont = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.regular) 正如您所知,我们可以在SwiftUI中使用如下字体: .font(.system(size: 35, weight: .ultraLight, design: .rounded)) Text("Example") .font(.appFont) 现在我想将这段代码作
let appFont = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.regular)
正如您所知,我们可以在SwiftUI中使用如下字体:
.font(.system(size: 35, weight: .ultraLight, design: .rounded))
Text("Example")
.font(.appFont)
现在我想将这段代码作为appFont常量,并使用.appFont调用这段代码,我们如何做到这一点?如果我正确理解了您的需求,可以通过扩展到View来完成,如
extension View {
func appFont() -> some View {
self.font(.system(size: 35, weight: .ultraLight, design: .rounded))
}
}
因此,在视图中的任何位置都可以将其用作
var body: some view {
SomeView().appFont()
}
您可以在扩展名中声明您的
Font
:
extension Font {
static let appFont = Font.system(size: 18, weight: .regular)
}
然后像这样使用它:
.font(.system(size: 35, weight: .ultraLight, design: .rounded))
Text("Example")
.font(.appFont)
我肯定这就是OP的意思,但它不是常数,对吧?
.system()
的结果不能根据环境而改变吗?谢谢@Asperi,我想保持不变。但是你的代码也在工作。