import SwiftUI // MARK: - 字体库枚举 /// 定义应用中可用的字体库 enum FontFamily: String, CaseIterable { case quicksandBold = "Quicksand-Bold" case quicksandRegular = "Quicksand-Regular" case inter = "Inter" case lavishlyYours = "LavishlyYours-Regular" /// 获取字体名称 var name: String { return self.rawValue } } // 为兼容旧代码,提供 `.quicksand` 的别名(已弃用) extension FontFamily { @available(*, deprecated, message: "Use .quicksandRegular instead.") static var quicksand: FontFamily { .quicksandRegular } } // MARK: - 文本样式枚举 /// 定义应用中使用的文本样式类型 enum TypographyStyle { case largeTitle // 大标题 case smallLargeTitle // 小大标题 case headline // 大标题 case headline1 // 大标题1 case title // 标题 case title2 // 标题 case title3 // 标题 case body // 正文 case subtitle // 副标题 case caption // 说明文字 case footnote // 脚注 case small // 小号文字 } // 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 = .quicksandRegular /// 文本样式配置表 private static let styleConfig: [TypographyStyle: TypographyConfig] = [ .largeTitle: TypographyConfig(size: 32, weight: .heavy, textStyle: .largeTitle), .smallLargeTitle: TypographyConfig(size: 30, weight: .heavy, textStyle: .largeTitle), .headline1: TypographyConfig(size: 26, weight: .bold, textStyle: .headline), .headline: TypographyConfig(size: 24, weight: .bold, textStyle: .headline), .title3: TypographyConfig(size: 22, weight: .semibold, textStyle: .title2), .title: TypographyConfig(size: 20, weight: .semibold, textStyle: .title2), .title2: TypographyConfig(size: 18, weight: .bold, textStyle: .title2), .body: TypographyConfig(size: 16, weight: .regular, textStyle: .body), .subtitle: TypographyConfig(size: 14, weight: .medium, textStyle: .subheadline), .caption: TypographyConfig(size: 12, weight: .regular, textStyle: .caption1), .footnote: TypographyConfig(size: 11, weight: .regular, textStyle: .footnote), .small: TypographyConfig(size: 10, weight: .regular, textStyle: .headline) ] // MARK: - 公共方法 /// 获取指定样式和字体库的字体 /// - Parameters: /// - style: 文本样式 /// - family: 字体库,默认为 nil 使用默认字体库 /// - Returns: 配置好的 Font 对象 static func font(for style: TypographyStyle, family: FontFamily? = nil, size: CGFloat? = nil) -> Font { let fontFamily = family ?? defaultFontFamily guard let config = styleConfig[style] else { return .body } // 尝试加载自定义字体 if let customFont = UIFont(name: fontFamily.name, size: size ?? config.size) { let metrics = UIFontMetrics(forTextStyle: config.textStyle) let scaledFont = metrics.scaledFont(for: customFont) return Font(scaledFont) } // 如果自定义字体加载失败,回退到系统字体 let systemFont = UIFont.systemFont(ofSize: size ?? 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 } }