wake-ios/wake/Typography.swift
2025-08-19 14:49:52 +08:00

96 lines
3.3 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import SwiftUI
// MARK: -
///
enum FontFamily: String, CaseIterable {
case sankeiCute = "SankeiCutePopanime" //
case quicksand = "Quicksand x" //
case quicksandBold = "Quicksand-Bold"
case quicksandRegular = "Quicksand-Regular"
// 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 //
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 = .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),
.small: TypographyConfig(size: 10, weight: .regular, textStyle: .headline)
]
// 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
}
}