107 lines
3.5 KiB
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?
|
|
}
|