在 Apple Developer 文档中,字体修饰符是这样声明的:
func font(_ font: Font?) -> some View
但是当在 SwiftUI 中将其调用为修饰符时,我们以这种方式使用:
Text("Hello, World!").font(.body)
// ↑
// Why do we add this dot?
为什么我们在body
之前添加那个点?
声明
struct 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)