jsdw_ios/QuickLocation/Core/Font/FontManager.swift

133 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.

//
// FontManager.swift
// QuickLocation
//
// Created by on 2026/8/5.
//
import UIKit
///
enum AppFont {
/// -Bold
case boboBold
///
case ziHunBianHei
///
case youSheBiaoTiHei
/// PostScript
var candidateNames: [String] {
switch self {
case .boboBold:
// TTF Full Name BoldPostScript -Bold
return ["荆南波波黑 Bold", "荆南波波黑-Bold", "JingNanBoBoHei-Bold"]
case .ziHunBianHei:
return ["zihunbiantaoti", "字魂扁黑体"]
case .youSheBiaoTiHei:
// PostScript / Full Name YouSheBiaoTiHei
return ["YouSheBiaoTiHei", "优设标题黑"]
}
}
var familyName: String? {
switch self {
case .boboBold: return "荆南波波黑"
case .ziHunBianHei: return nil
case .youSheBiaoTiHei: return "YouSheBiaoTiHei"
}
}
var fileName: String {
switch self {
case .boboBold: return "荆南波波黑-Bold.ttf"
case .ziHunBianHei: return "zihunbiantaoti-r.ttf"
case .youSheBiaoTiHei: return "优设标题黑_猫啃网.ttf"
}
}
var fallbackWeight: UIFont.Weight {
switch self {
case .boboBold: return .bold
case .ziHunBianHei: return .medium
case .youSheBiaoTiHei: return .bold
}
}
}
/// +
enum FontManager {
private static var resolvedNames: [AppFont: String] = [:]
private static var didRegister = false
/// 退
static func font(_ style: AppFont, size: CGFloat) -> UIFont {
ensureRegistered()
if let name = resolvedFontName(for: style),
let custom = UIFont(name: name, size: size) {
return custom
}
return .systemFont(ofSize: size, weight: style.fallbackWeight)
}
static func boboBold(_ size: CGFloat) -> UIFont {
font(.boboBold, size: size)
}
static func ziHunBianHei(_ size: CGFloat) -> UIFont {
font(.ziHunBianHei, size: size)
}
static func youSheBiaoTiHei(_ size: CGFloat) -> UIFont {
font(.youSheBiaoTiHei, size: size)
}
/// Bundle
static func registerCustomFonts() {
for style in [AppFont.boboBold, .ziHunBianHei, .youSheBiaoTiHei] {
if let registered = UIFont.loadFontName(style.fileName), !registered.isEmpty {
// PostScript "-Bold"
if !registered.hasPrefix("-"),
UIFont(name: registered, size: 12) != nil {
resolvedNames[style] = registered
continue
}
}
if let name = resolveAvailableName(for: style) {
resolvedNames[style] = name
}
}
didRegister = true
}
private static func ensureRegistered() {
guard !didRegister else { return }
registerCustomFonts()
}
private static func resolvedFontName(for style: AppFont) -> String? {
if let cached = resolvedNames[style] { return cached }
if let name = resolveAvailableName(for: style) {
resolvedNames[style] = name
return name
}
return nil
}
private static func resolveAvailableName(for style: AppFont) -> String? {
for name in style.candidateNames {
if UIFont(name: name, size: 12) != nil {
return name
}
}
if let family = style.familyName {
let names = UIFont.fontNames(forFamilyName: family)
if let first = names.first {
return first
}
}
return nil
}
}