wake-ios/wake/Typography.swift
2025-09-01 18:35:57 +08:00

110 lines
4.0 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 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
}
}