feat: 设置全局字体样式

This commit is contained in:
jinyaqiu 2025-08-15 22:37:04 +08:00
parent 50bff7cf12
commit 8eecd42076
6 changed files with 116 additions and 1 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -52,7 +52,30 @@ struct ContentView: View {
Spacer() //
}
Spacer()
VStack(alignment: .leading, spacing: 16) {
Text("这是主标题")
.font(Typography.font(for: .headline))
Text("这是次级标题")
.font(Typography.font(for: .title))
Text("这是正文内容,适用于段落和说明文字。")
.font(Typography.font(for: .body))
Text("这是副标题")
.font(Typography.font(for: .subtitle))
.foregroundColor(.secondary)
Text("ABCDEFG").font(Typography.font(for: .headline))
Text("脚注信息")
.font(Typography.font(for: .footnote))
}
.padding()
.onAppear {
//
print("当前字体缩放级别: \(UIView.appearance().traitCollection.preferredContentSizeCategory)")
}
//
List {
Section(header: Text("我的收藏")) {

View File

@ -4,6 +4,7 @@
<dict>
<key>UIAppFonts</key>
<array>
<string>Quicksand x.ttf</string>
<string>SankeiCutePopanime.ttf</string>
</array>
</dict>

Binary file not shown.

91
wake/Typography.swift Normal file
View File

@ -0,0 +1,91 @@
import SwiftUI
// MARK: -
///
enum FontFamily: String, CaseIterable {
case sankeiCute = "SankeiCutePopanime" //
case quicksand = "Quicksand x" //
// case
// : case anotherFont = "AnotherFontName"
///
var name: String {
return self.rawValue
}
}
// MARK: -
/// 使
enum TypographyStyle {
case headline //
case title //
case body //
case subtitle //
case caption //
case footnote //
}
// MARK: -
private struct TypographyConfig {
let size: CGFloat
let weight: UIFont.Weight
let textStyle: UIFont.TextStyle
}
// MARK: - Typography
struct Typography {
// MARK: -
///
private static let defaultFontFamily: FontFamily = .quicksand
///
private static let styleConfig: [TypographyStyle: TypographyConfig] = [
.headline: TypographyConfig(size: 24, weight: .bold, textStyle: .headline),
.title: TypographyConfig(size: 20, weight: .semibold, textStyle: .title2),
.body: TypographyConfig(size: 16, weight: .regular, textStyle: .body),
.subtitle: TypographyConfig(size: 14, weight: .medium, textStyle: .subheadline),
.caption: TypographyConfig(size: 12, weight: .light, textStyle: .caption1),
.footnote: TypographyConfig(size: 11, weight: .regular, textStyle: .footnote)
]
// MARK: -
///
/// - Parameters:
/// - style:
/// - family: nil 使
/// - Returns: Font
static func font(for style: TypographyStyle, family: FontFamily? = nil) -> Font {
let fontFamily = family ?? defaultFontFamily
guard let config = styleConfig[style] else {
return .body
}
//
if let customFont = UIFont(name: fontFamily.name, size: config.size) {
let metrics = UIFontMetrics(forTextStyle: config.textStyle)
let scaledFont = metrics.scaledFont(for: customFont)
return Font(scaledFont)
}
// 退
let systemFont = UIFont.systemFont(ofSize: config.size, weight: config.weight)
let metrics = UIFontMetrics(forTextStyle: config.textStyle)
let scaledFont = metrics.scaledFont(for: systemFont)
return Font(scaledFont)
}
///
/// - Returns:
static func availableFonts() -> [FontFamily] {
return FontFamily.allCases
}
///
/// - Parameter family:
/// - Returns:
static func isFontAvailable(_ family: FontFamily) -> Bool {
return UIFont(name: family.name, size: 16) != nil
}
}