// // Typography+Additional.swift // VDS // // Created by Matt Bruce on 7/21/23. // import Foundation import UIKit import VDSCoreTokens //MARK: Alignments extension TextStyle { /// Alignments Available for the TextStyle public var aligments: [TextAlignment] { return [.left, .center] } } //MARK: Fonts extension TextStyle { /// Font for the TextStyle. public var font: UIFont { return fontFace.font(ofSize: pointSize) } } //MARK: Style Helper extension TextStyle { /// Default style used if no style is given. public static var defaultStyle: TextStyle { return bodyLarge } /// Find a TextStyle based off a Name. /// - Parameter name: Name of the TextStyle you want to find. /// - Returns: TextStyle for the given name. public static func textStyle(for name: String) -> TextStyle? { guard let style = TextStyle.allCases.first(where: {$0.rawValue == name }) else { return nil } return style } /// Returns a TextStyle based off a Font-Name and Size /// - Parameters: /// - fontName: Name of the font you want to find. /// - size: PointSize of the Font you want to find. /// - Returns: The exact TextStyle based on the FontName and Point Size. public static func style(for fontName: String, size: CGFloat) -> TextStyle? { //filter all styles by fontName let styles = allCases.filter{$0.fontFace.fontName == fontName }.sorted { lhs, rhs in lhs.pointSize < rhs.pointSize } //if there are no styles then return nil guard styles.count > 0 else { return nil } //if there is an exact match on a style with this pointSize then return it if let style = styles.first(where: {$0.pointSize == size }) { return style } else if let largerIndex = styles.firstIndex(where: { $0.pointSize > size}) { //find the closet one to pointSize return styles[max(largerIndex - 1, 0)] } else { //return the last style return styles.last! } } } extension TextStyle: CustomDebugStringConvertible { /// Used for showing the TextStyle Properties. public var debugDescription: String { "Name: \(self.rawValue) FontFace: \(font.fontName) FontWeight: \(self.rawValue.hasPrefix("bold") ? "bold" : "normal") PointSize: \(font.pointSize) LetterSpacing: \(letterSpacing) LineHeight: \(lineHeight)" } }