如何在 SwiftUI 中使用 SF Pro Rounded 或自定义导航标题字体?

Cha*_*hal 3 swift swiftui

我正在尝试将导航视图标题的字体更改为系统圆角字体,但我似乎找不到办法来做到这一点。

需要明确的是,我可以在任何文本视图、列表等上很好地更改字体,但在导航标题上这似乎是不可能的。

作为参考,Carrot Weather 应用程序中的标题(“设置”)实现了这一点:在此输入图像描述

任何想法?

谢谢 !!!

ahe*_*eze 9

您可以使用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+
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

圆形导航栏标题