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

124 lines
4.2 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.

//
// 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?
}