【问题标题】:Change default system font in SwiftUI在 SwiftUI 中更改默认系统字体
【发布时间】:2019-11-13 17:36:23
【问题描述】:

我正在尝试在我的 SwiftUI 应用程序中设置自定义默认字体。我从这个线程Set a default font for whole iOS app?尝试了几个建议。

但是,这些似乎都不适用于 SwiftUI。例如使用这种方法:

// Constants.swift
struct Fonts {
    static var plex = "IBMPlexMono-Text"
}


// FontExtension.swift
extension UILabel {
    var substituteFontName : String {
        get { return self.font.fontName }
        set { self.font = UIFont(name: Fonts.plex, size: 17)! }
    }

}

// AppDelegate.swift in didFinishLaunchingWithOptions-function
UILabel.appearance().substituteFontName = Fonts.plex

当我启动应用程序时,自定义字体会出现片刻,然后变回 Apple 的默认字体。为什么要改回苹果的字体,怎么永久改回?

是否可以在Text-View 上进行扩展?

【问题讨论】:

  • 它有一个 .font()。 // 字体(Font(CTFont)) 声明
  • 从我对this post的回答中尝试方法
  • @Asperi 谢谢你的链接。建议的解决方案非常简单有效。但是,它不会将所有 Text 视图更改为新字体。例如,导航栏中的标题不会使用新字体更新。
  • NavigationView 其实是个特例,目前只能通过 UINavigationBar.appearance() 来配置。例如。字体为UINavigationBar.appearance().largeTitleTextAttributes = [.font: UIFont(name: "Arial", size: 32)!]
  • 好的,谢谢,这适用于大标题。对于内联标题,我使用UINavigationBar.appearance().titleTextAttributes = [.font : UIFont(name: Fonts.plexSemiBold, size: 20)!] 而对于后退按钮,我想我使用.navigationBarBackButtonHidden(true) 并实现自定义NavigationBarItem。然后应该更新所有文本。有点痛苦,但它有效。

标签: swiftui


【解决方案1】:

您可以像这样使用自定义字体:

Font.custom("Font-Family-Name", size: fontSize)

例子:

Text("Testing")
.font(Font.custom("Font-Family-Name", size: 16))

要在应用程序的任何位置使用字体,请创建如下结构。请务必在包含字体结构的文件中导入 SwiftUI:

导入 SwiftUI

struct AppFont {
    static func commonFont(fontSize: CGFloat) -> Font {
        return Font.custom("Font-Family-Name", size: fontSize)
    }
}

现在您可以像这样在应用中的任何位置使用它们:

Text("Testing")
.font(AppFont.commonFont(fontSize: 16))

【讨论】:

    【解决方案2】:

    你可以拥有:

    extension Font {
        static let mediumFont = Font.custom("Sans-Regular", size: Font.TextStyle.subheadline.size, relativeTo: .caption)
        static let mediumSmallFont = Font.custom("Sans-Regular", size: Font.TextStyle.footnote.size, relativeTo: .caption)
        static let smallFont = Font.custom("Sans-Regular", size: Font.TextStyle.caption.size, relativeTo: .caption)
        static let verySmallFont = Font.custom("Sans-Regular", size: Font.TextStyle.caption2.size, relativeTo: .caption)
    }
    
    extension Font.TextStyle {
        var size: CGFloat {
            switch self {
            case .largeTitle: return 60
            case .title: return 48
            case .title2: return 34
            case .title3: return 24
            case .headline, .body: return 18
            case .subheadline, .callout: return 16
            case .footnote: return 14
            case .caption: return 12
            case .caption2: return 10
            @unknown default:
                return 8
            }
        }
    }
    

    并像这样使用它:

    Text("Edit Profile")
       .font(.mediumSmallFont)
    

    【讨论】:

      猜你喜欢
      • 2013-07-29
      • 2019-12-11
      • 2011-10-31
      • 2020-03-17
      • 2011-11-05
      • 1970-01-01
      • 2021-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多