在 Apple Developer 文档中,据说字体修饰符是这样声明的:
func font(_ font: Font?) -> some View
但是当在 SwiftUI 中调用它作为修饰符时,我们是这样使用的:
Text("Hello, World!").font(.body)
// ↑
// Why do we add this dot?
为什么我们要在前面加上那个点body?
宣言
struct Font
系统在给定环境中使用字体时解析字体的值,因为 Font 是后期绑定标记。
您可以通过 Font 静态属性获取标准字体
static let largeTitle: Font
static let title: Font
static var headline: Font
static var subheadline: Font
static var body: Font
static var callout: Font
static var caption: Font
static var footnote: Font
写作
Text("some text").font(.title)
是相同的
Text("some text").font(Font.title)