From 4c38676f59d458c62479edda0d6d161054206c2d Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Fri, 5 Aug 2022 09:01:26 -0500 Subject: [PATCH] refactored more fonts Signed-off-by: Matt Bruce --- VDS/Components/Label/Label.swift | 4 ++-- VDS/Fonts/FontStyles.swift | 30 ++++++++++++++++++++++++++---- VDS/Protocols/Fontable.swift | 12 ++++++++++++ 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/VDS/Components/Label/Label.swift b/VDS/Components/Label/Label.swift index 87687ec9..cf53996b 100644 --- a/VDS/Components/Label/Label.swift +++ b/VDS/Components/Label/Label.swift @@ -119,10 +119,10 @@ open class LabelBase: UILabel, ModelHandlerable, Initable textAlignment = viewModel.textPosition.textAlignment textColor = textColorHelper.getColor(viewModel) - if let vdsFont = try? FontStyle.font(for: viewModel.fontCategory, fontWeight: viewModel.fontWeight, fontSize: viewModel.fontSize) { + if let vdsFont = self.font { font = vdsFont } else { - font = FontStyle.RegularBodyLarge.font + font = FontStyle.defaultStyle.font } if let attributes = viewModel.attributes, let text = model.text, let font = font, let textColor = textColor { diff --git a/VDS/Fonts/FontStyles.swift b/VDS/Fonts/FontStyles.swift index 6eab39af..03839274 100644 --- a/VDS/Fonts/FontStyles.swift +++ b/VDS/Fonts/FontStyles.swift @@ -42,6 +42,19 @@ public enum FontCategory: String, Codable, CaseIterable { case body case micro + var fontSizes: [FontSize] { + switch self { + case .feature: + return [.xlarge, .medium, .small, .xsmall] + case .title: + return [.xxlarge, .xlarge, .large, .medium, .small] + case .body: + return [.large, .medium, .small, .xsmall] + case .micro: + return [] + } + } + var titleCase: String { switch self { case .feature: return "Feature" @@ -74,7 +87,7 @@ public enum FontSize: String, Codable, CaseIterable { public enum FontStyle: String, Codable, CaseIterable { public enum Error: Swift.Error { - case fontNotFound + case styleNotFound(fontCatergory: FontCategory, fontSize: FontSize, availableFontSizes: [FontSize]) } case RegularFeatureXLarge @@ -105,6 +118,7 @@ public enum FontStyle: String, Codable, CaseIterable { case BoldBodyMedium case RegularBodySmall case BoldBodySmall + case RegularMicro case BoldMicro @@ -181,6 +195,10 @@ public enum FontStyle: String, Codable, CaseIterable { } } + public static var defaultStyle: FontStyle { + return FontStyle.RegularBodyLarge + } + public var font: UIFont { return fontFace.font(ofSize: pointSize) } @@ -189,11 +207,15 @@ public enum FontStyle: String, Codable, CaseIterable { return fontFace.font(ofSize: pointSize / 2) } - public static func font(for category: FontCategory, fontWeight: FontWeight, fontSize: FontSize, isSuperScript: Bool = false) throws -> UIFont { + public static func style(for category: FontCategory, fontWeight: FontWeight, fontSize: FontSize) throws -> FontStyle { let fontName = "\(fontWeight.titleCase)\(category.titleCase)\(fontSize.titleCase)" guard let fontStyle = FontStyle(rawValue: fontName) else { - throw FontStyle.Error.fontNotFound + throw FontStyle.Error.styleNotFound(fontCatergory: category, fontSize: fontSize, availableFontSizes: category.fontSizes) } - return fontStyle.font + return fontStyle + } + + public static func font(for category: FontCategory, fontWeight: FontWeight, fontSize: FontSize) throws -> UIFont { + return try style(for: category, fontWeight: fontWeight, fontSize: fontSize).font } } diff --git a/VDS/Protocols/Fontable.swift b/VDS/Protocols/Fontable.swift index a2b6beb3..493b2deb 100644 --- a/VDS/Protocols/Fontable.swift +++ b/VDS/Protocols/Fontable.swift @@ -6,9 +6,21 @@ // import Foundation +import UIKit public protocol Fontable { var fontSize: FontSize { get set } var fontWeight: FontWeight { get set } var fontCategory: FontCategory { get set } } + +extension Fontable { + + var style: FontStyle? { + return try? FontStyle.style(for: fontCategory, fontWeight: fontWeight, fontSize: fontSize) + } + + var font: UIFont? { + return self.style?.font + } +}