SwiftUI 中文本的自定义字体大小

在我的视图中有一个标签,我想使用中等大小的系统字体,大小为21点。
I created a custom extension to re-use the font created:

extension Font {
static var primaryButton: Font {
return Font.custom("SFUIDisplay-Light", size: 21)
}
}

但是,这没有任何效果。我将字符串更改为 HelveticaNeue-UltraLight,它确实起作用了,所以我猜测 SFUIDisplay-Light只是字体名称不正确。
在字体书,它说 SFProText-Light,但这也不适合我。

在 SwiftUI 中旧金山字体的正确字体名称是什么?
或者: 如何使用系统字体设置字体大小?

116879 次浏览

您可以使用以下方法设置系统字体的显式大小:

.font(.system(size: 60))

您可以像这样设置自定义字体大小,

.font(.custom("FONT_NAME", size: 20))
// Using system font "San Francisco"
let fontSystem = Font.system(size: 13.0)


// Using installed custom font
let fontCustom = Font.custom("YOUR FONT NAME", size: 13.0)

您可以将 font*常量放在需要设置字体样式的位置。

阅读更多 https://developer.apple.com/documentation/swiftui/font中的“获取系统字体”部分

Text("Default font design").font(Font.system(size:30, design: .default))
Text("Monospaced font design").font(Font.system(size:30, design: .monospaced))
Text("Rounded font design").font(Font.system(size:30, design: .rounded))
Text("Serif font design").font(Font.system(size:30, design: .serif))


Text("Large Title").font(.largeTitle)
Text("Title").font(.title)
Text("Headline").font(.headline)
Text("SubHeadline").font(.subheadline)
Text("Body").font(.body)
Text("Callout").font(.callout)
Text("Caption").font(.caption)
Text("FootNote").font(.footnote)


Text("Custom font").font(Font.custom("OpenSans-Bold", size: 12.3))

详情请参阅: https://github.com/arjun011/SwiftUI-Controls

如果你想为你的文本设置一个自定义字体,比如说 Helvetica Neue,那么在这种情况下,你可以这样做

Text("Hello World")
.font(.custom("Helvetica Neue", size: 20))

如果你只想使用系统字体,那么在这种情况下,你可以这样做

Text("Hello World")
.font(.system(size: 20, weight: .light, design: .default))