124 lines
4.2 KiB
Swift
124 lines
4.2 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: 字体文件名称(可含扩展名,如 `xxx.ttf`)
|
||
/// - Returns: 注册后的字体 Full Name(可用于 `UIFont(name:)`)
|
||
public static func loadFontName(_ fileName: String) -> String? {
|
||
let nsName = fileName as NSString
|
||
let resource = nsName.deletingPathExtension
|
||
let ext = nsName.pathExtension
|
||
let fontURL: URL?
|
||
if ext.isEmpty {
|
||
fontURL = Bundle.main.url(forResource: fileName, withExtension: nil)
|
||
} else {
|
||
fontURL = Bundle.main.url(forResource: resource, withExtension: ext)
|
||
?? Bundle.main.url(forResource: fileName, withExtension: nil)
|
||
}
|
||
guard let fontURL,
|
||
let data = try? Data(contentsOf: fontURL),
|
||
let provider = CGDataProvider(data: data as CFData),
|
||
let font = CGFont(provider) else { return nil }
|
||
|
||
var error: Unmanaged<CFError>?
|
||
CTFontManagerRegisterGraphicsFont(font, &error)
|
||
|
||
// Full Name 比损坏的 PostScript Name 更可靠
|
||
if let full = font.fullName as String?, !full.isEmpty {
|
||
return full
|
||
}
|
||
if let ps = font.postScriptName as String?, !ps.isEmpty, !ps.hasPrefix("-") {
|
||
return ps
|
||
}
|
||
return nil
|
||
}
|
||
}
|
||
// 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?
|
||
}
|