SwiftUI 中 Text 对象中使用的当前字体 [英] Current font used in a Text object in SwiftUI
本文介绍了SwiftUI 中 Text 对象中使用的当前字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在 SwiftUI 中获取/获取 Text 对象中使用的当前字体?前任.Text("abc").font()
不起作用.
Is there a way to get/obtain the current font used in a Text object in SwiftUI? ex. Text("abc").font()
does not work.
推荐答案
当前字体可从环境访问:
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())
}
}
请参阅 https://developer.apple.com/documentation/swiftui/environmentvalues 有关可用密钥的完整列表,它们可以派上用场.
See https://developer.apple.com/documentation/swiftui/environmentvalues for the full list of available keys, they can come in handy.
这篇关于SwiftUI 中 Text 对象中使用的当前字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文