gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:30:31 25 4
gpt4 key购买 nike

在 SwiftUI 中,您可以获得所有这些方便的小字体便利访问器,例如 Font.caption , Font.title , Font.body , 等等..

例如

VStack {
Text("Some Title").font(Font.title)
Text("Some Caption Text").font(Font.caption)
}

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

最佳答案

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

    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)
}
}

关于ios - 如何在 SwiftUI 中将自定义字体系列设置为整个应用程序的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58375481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com