如何在 SwiftUI 中将自定义字体系列设置为整个应用程序的默认值

Met*_*ete 4 ios swiftui

在SwiftUI你得到所有这些方便的小字体方便存取像Font.captionFont.titleFont.body,等。

例如

VStack {
 Text("Some Title").font(Font.title)
 Text("Some Caption Text").font(Font.caption)
}
Run Code Online (Sandbox Code Playgroud)

它们都为默认的 Helvetica 字体系列指定了不同的字体样式。我想使用这些非常有用的便捷访问器,而无需在我的应用程序中使用 Helvetica。我可以更改默认字体系列吗?或者我是否经常需要应用自定义字体,例如:

Text("Custom Text").font(Font.custom("SourceSansPro-Regular", size: 14.0)

Met*_*ete 11

我当前的方法是重新创建我自己的字体工厂:

struct MyFont {
  static let title = Font.custom("SourceSansPro-Bold", size: 24.0)
  static let body = Font.custom("SourceSansPro-Regular", size: 12.0)
}
Run Code Online (Sandbox Code Playgroud)

然后使用egMyFont.title代替Font.title


Jus*_*ler 9

如果需要,您可以覆盖系统字体。

    extension Font {
    
    /// Create a font with the large title text style.
    public static var largeTitle: Font {
        return Font.custom("OpenSans-Regular", size: UIFont.preferredFont(forTextStyle: .largeTitle).pointSize)
    }

    /// Create a font with the title text style.
    public static var title: Font {
        return Font.custom("OpenSans-Regular", size: UIFont.preferredFont(forTextStyle: .title1).pointSize)
    }

    /// Create a font with the headline text style.
    public static var headline: Font {
        return Font.custom("OpenSans-Regular", size: UIFont.preferredFont(forTextStyle: .headline).pointSize)
    }

    /// Create a font with the subheadline text style.
    public static var subheadline: Font {
        return Font.custom("OpenSans-Light", size: UIFont.preferredFont(forTextStyle: .subheadline).pointSize)
    }

    /// Create a font with the body text style.
    public static var body: Font {
           return Font.custom("OpenSans-Regular", size: UIFont.preferredFont(forTextStyle: .body).pointSize)
       }

    /// Create a font with the callout text style.
    public static var callout: Font {
           return Font.custom("OpenSans-Regular", size: UIFont.preferredFont(forTextStyle: .callout).pointSize)
       }

    /// Create a font with the footnote text style.
    public static var footnote: Font {
           return Font.custom("OpenSans-Regular", size: UIFont.preferredFont(forTextStyle: .footnote).pointSize)
       }

    /// Create a font with the caption text style.
    public static var caption: Font {
           return Font.custom("OpenSans-Regular", size: UIFont.preferredFont(forTextStyle: .caption1).pointSize)
       }

    public static func system(size: CGFloat, weight: Font.Weight = .regular, design: Font.Design = .default) -> Font {
        var font = "OpenSans-Regular"
        switch weight {
        case .bold: font = "OpenSans-Bold"
        case .heavy: font = "OpenSans-ExtraBold"
        case .light: font = "OpenSans-Light"
        case .medium: font = "OpenSans-Regular"
        case .semibold: font = "OpenSans-SemiBold"
        case .thin: font = "OpenSans-Light"
        case .ultraLight: font = "OpenSans-Light"
        default: break
        }
        return Font.custom(font, size: size)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这会导致 SwiftUI 实时预览在 Xcode 11.5 中崩溃。 (3认同)
  • 对于“Text”等常规元素效果很好,但不适用于“Section”标题或“navigationBarTitle”,知道如何实现这一点吗? (2认同)