// // FontStyle.swift // VDS // // Created by Matt Bruce on 7/27/22. // import Foundation import UIKit /// Used in Classes that require Fonts public protocol FontProtocol { var fontFileExtension: String { get } var fontName: String { get } static var allCases: [Self] { get } } extension FontProtocol { /// Registers the fonts used in the VDS Framework. public func register() { guard let bundle = Bundle(identifier: "com.vzw.vds") else { return } Self.allCases.forEach{ font in if let url = bundle.url(forResource: font.fontName, withExtension: font.fontFileExtension){ do{ try Self.register(from: url) } catch{ //TODO: error ("Font called \(font.fontName).\(font.fontFileExtension) couldn't be registered") } } else { //TODO: error ("Font for FontStyle: \(font) couldn't be found") } } } /// Registers a font from a fileLocation /// - Parameter url: URL of the file location. public static func register(from url: URL) throws { guard let fontDataProvider = CGDataProvider(url: url as CFURL) else { throw NSError(domain: "www.verizon.com", code: 123, userInfo: ["description":"Could not create font data provider for \(url)."]) } let font = CGFont(fontDataProvider) var error: Unmanaged? guard CTFontManagerRegisterGraphicsFont(font!, &error) else { throw error!.takeUnretainedValue() } } /// Returns a UIFont for the fontName and size given. /// - Parameters: /// - size: Size of the font /// - Returns: UIFont for the fontName and Size. public func font(ofSize size: CGFloat) -> UIFont{ DispatchQueue.once(block: { self.register() }) guard let found = UIFont(name: self.fontName, size: size) else { return .systemFont(ofSize: size) } return found } } extension UIFont { public var scaledFont: UIFont { UIFontMetrics.default.scaledFont(for: self) } }