【问题标题】:How can I use SF Pro Rounded or a custom Font for Navigation titles in SwiftUI?如何在 SwiftUI 中为导航标题使用 SF Pro Rounded 或自定义字体?
【发布时间】:2021-07-17 01:47:34
【问题描述】:

我正在尝试将导航视图标题的字体更改为系统四舍五入的字体,但我似乎找不到方法。

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

为了参考应用 Carrot Weather 中的标题(“设置”)实现了这一点:

有什么想法吗?

谢谢!!!

【问题讨论】:

标签: swift swiftui


【解决方案1】:

您可以使用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+
        }
    }
}

结果:

【讨论】:

    猜你喜欢
    • 2019-10-26
    • 2023-02-08
    • 1970-01-01
    • 2019-10-28
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多