jsdw_ios/QuickLocation/Core/Extension/UIFont+Extension.swift

107 lines
3.5 KiB
Swift

//
// UIFont+Extension.swift
// DLSDK
//
// Created by osell on 2023/4/11.
//
import UIKit
// MARK: -
public extension UIFont {
///
/// - Parameters:
/// - style:
/// - size:
convenience init?<Style: FontPlugin>(custom style: Style, size: CGFloat) {
var name = style.name
if UIFont.fontNames(forFamilyName: style.name).isEmpty,
let fontName = UIFont.loadFontName(style.fileName) {
name = fontName
}
self.init(name: name, size: size)
}
///
/// - Parameters:
/// - style:
/// - size:
/// - Returns: UIFont
static func customFont<Style: FontPlugin>(style: Style, size: CGFloat) -> UIFont? {
UIFont(custom: style, size: size)
}
///
/// - Parameter name:
private static func loadFontName(_ fileName: String) -> String? {
//
guard let fontURL = Bundle.main.url(forResource: fileName, withExtension: nil),
let data = try? Data(contentsOf: fontURL),
let provider = CGDataProvider(data: data as CFData),
let font = CGFont(provider) else { return nil }
//
CTFontManagerRegisterGraphicsFont(font, nil)
return String(font.fullName ?? "")
}
}
// MARK: - Montserrat
//public extension UIFont {
//
// static func montserratLight(size: CGFloat) -> UIFont {
// Montserrat.light.font(ofSize: size) ?? UIFont.systemFont(ofSize: size, weight: .light)
// }
//
// static func montserratRegular(size: CGFloat) -> UIFont {
// Montserrat.regular.font(ofSize: size) ?? UIFont.systemFont(ofSize: size, weight: .regular)
// }
//
// static func montserratMedium(size: CGFloat) -> UIFont {
// Montserrat.medium.font(ofSize: size) ?? UIFont.systemFont(ofSize: size, weight: .medium)
// }
//
// static func montserratSemiBold(size: CGFloat) -> UIFont {
// Montserrat.semiBold.font(ofSize: size) ?? UIFont.systemFont(ofSize: size, weight: .semibold)
// }
//
// static func montserratBold(size: CGFloat) -> UIFont {
// Montserrat.bold.font(ofSize: size) ?? UIFont.systemFont(ofSize: size, weight: .bold)
// }
//}
// MARK: - DINPro
//public extension UIFont {
//
// static func dinProLight(size: CGFloat) -> UIFont {
// DINPro.light.font(ofSize: size) ?? UIFont.systemFont(ofSize: size, weight: .light)
// }
//
// static func dinProRegular(size: CGFloat) -> UIFont {
// DINPro.regular.font(ofSize: size) ?? UIFont.systemFont(ofSize: size, weight: .regular)
// }
//
// static func dinProMedium(size: CGFloat) -> UIFont {
// DINPro.medium.font(ofSize: size) ?? UIFont.systemFont(ofSize: size, weight: .medium)
// }
//
// static func dinProBlack(size: CGFloat) -> UIFont {
// DINPro.black.font(ofSize: size) ?? UIFont.systemFont(ofSize: size, weight: .black)
// }
//
// static func dinProBold(size: CGFloat) -> UIFont {
// DINPro.bold.font(ofSize: size) ?? UIFont.systemFont(ofSize: size, weight: .bold)
// }
//}
// MARK: - FontPlugin
public protocol FontPlugin {
///
var name: String { get }
///
var fileName: String { get }
///
func font(ofSize: CGFloat) -> UIFont?
}