我正在尝试将导航 View 标题的字体更改为系统圆角字体,但我似乎找不到办法。
要清楚,我可以在任何 TextView 、列表等上很好地更改字体,但在导航标题上这似乎是不可能的。
作为引用,Carrot Weather 应用程序中的标题(“设置”)实现了这一点:
有什么想法吗?
谢谢!!!
最佳答案
您可以使用 largeTitle
获取默认导航栏字体,然后使用 withDesign(_:)
应用舍入.
struct ContentView: View {
init() {
var titleFont = UIFont.preferredFont(forTextStyle: .largeTitle) /// the default large title font
titleFont = UIFont(
descriptor:
titleFont.fontDescriptor
.withDesign(.rounded)? /// make rounded
.withSymbolicTraits(.traitBold) /// make bold
??
titleFont.fontDescriptor, /// return the normal title if customization failed
size: titleFont.pointSize
)
/// set the rounded font
UINavigationBar.appearance().largeTitleTextAttributes = [.font: titleFont]
}
var body: some View {
NavigationView {
Text("Hello World!")
.navigationTitle("Dashboard") /// use this for iOS 14+
}
}
}
结果:
关于swift - 如何在 SwiftUI 中使用 SF Pro Rounded 或自定义字体作为导航标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68416796/