feat: 设置全局字体样式
This commit is contained in:
parent
50bff7cf12
commit
8eecd42076
Binary file not shown.
@ -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("我的收藏")) {
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
<dict>
|
||||
<key>UIAppFonts</key>
|
||||
<array>
|
||||
<string>Quicksand x.ttf</string>
|
||||
<string>SankeiCutePopanime.ttf</string>
|
||||
</array>
|
||||
</dict>
|
||||
|
||||
BIN
wake/Resources/Quicksand x.ttf
Normal file
BIN
wake/Resources/Quicksand x.ttf
Normal file
Binary file not shown.
91
wake/Typography.swift
Normal file
91
wake/Typography.swift
Normal 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
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user