问题
Is there a way to get/obtain the current font used in a Text object in SwiftUI? ex. Text("abc").font() does not work.
回答1:
The current font is accessible from the environment:
struct ChildView: View {
@Environment(\.font) var font
var body: some View {
Text("Italic version of the hierarchy's font")
.font((font ?? .body).italic())
}
}
See https://developer.apple.com/documentation/swiftui/environmentvalues for the full list of available keys, they can come in handy.
回答2:
You can use systemFont.
Text("ddd").font(Font.system(size: 50))
So you don't need to know exact name of the font.
来源:https://stackoverflow.com/questions/58681801/current-font-used-in-a-text-object-in-swiftui