diff --git a/VDS/Components/TitleLockup/TitleLockupTextStyle.swift b/VDS/Components/TitleLockup/TitleLockupTextStyle.swift index e8dc0319..a09491ae 100644 --- a/VDS/Components/TitleLockup/TitleLockupTextStyle.swift +++ b/VDS/Components/TitleLockup/TitleLockupTextStyle.swift @@ -11,7 +11,7 @@ extension TitleLockup { //-------------------------------------------------- // MARK: - Enums //-------------------------------------------------- - public enum TitleTextStyle: String, EnumSubset { + public enum TitleTextStyle: String, CaseIterable { case featureMedium case boldFeatureMedium @@ -32,9 +32,14 @@ extension TitleLockup { case boldTitleSmall public var defaultValue: TextStyle {.boldFeatureXSmall } + + public var value: TextStyle { + TextStyle.textStyle(for: self.rawValue) ?? defaultValue + } + } - public enum OtherTextStyle: String, EnumSubset { + public enum OtherTextStyle: String, CaseIterable { case bodyLarge case boldBodyLarge case bodyMedium @@ -43,6 +48,9 @@ extension TitleLockup { case boldBodySmall public var defaultValue: TextStyle {.bodyLarge } + + public var value: TextStyle { + TextStyle.textStyle(for: self.rawValue) ?? defaultValue + } } - } diff --git a/VDS/Typography/Typography.swift b/VDS/Typography/Typography.swift index 0df38e84..8d815ad3 100644 --- a/VDS/Typography/Typography.swift +++ b/VDS/Typography/Typography.swift @@ -23,45 +23,230 @@ public enum TextPosition: String, CaseIterable { } } -public enum TextStyle: String, CaseIterable { +public struct TextStyle: Equatable { + public let rawValue: String + public let pointSize: CGFloat + public let lineHeight: CGFloat + public let letterSpacing: CGFloat + public let fontFace: Fonts - case featureXLarge - case boldFeatureXLarge - case featureLarge - case boldFeatureLarge - case featureMedium - case boldFeatureMedium - case featureSmall - case boldFeatureSmall - case featureXSmall - case boldFeatureXSmall - - case title2XLarge - case boldTitle2XLarge - case titleXLarge - case boldTitleXLarge - case titleLarge - case boldTitleLarge - case titleMedium - case boldTitleMedium - case titleSmall - case boldTitleSmall - - case bodyLarge - case boldBodyLarge - case bodyMedium - case boldBodyMedium - case bodySmall - case boldBodySmall - - case micro - case boldMicro - - public static var defaultStyle: TextStyle { - return .bodyLarge + public init(rawValue: String, fontFace: Fonts, pointSize: CGFloat, lineHeight: CGFloat, letterSpacing: CGFloat) { + self.rawValue = rawValue + self.fontFace = fontFace + self.pointSize = pointSize + self.lineHeight = lineHeight + self.letterSpacing = letterSpacing } } +//MARK: Definitions +extension TextStyle { + + // Static properties for different text styles + public static let featureXLarge = TextStyle(rawValue: "featureXLarge", + fontFace: .dsLight, + pointSize: UIDevice.isIPad ? VDSTypography.fontSizeFeature144 : VDSTypography.fontSizeFeature96, + lineHeight: UIDevice.isIPad ? VDSTypography.lineHeightFeature136 : VDSTypography.lineHeightFeature88, + letterSpacing: 0.25) + + public static let boldFeatureXLarge = TextStyle(rawValue: "boldFeatureXLarge", + fontFace: .dsBold, + pointSize: UIDevice.isIPad ? VDSTypography.fontSizeFeature144 : VDSTypography.fontSizeFeature96, + lineHeight: UIDevice.isIPad ? VDSTypography.lineHeightFeature136 : VDSTypography.lineHeightFeature88, + letterSpacing: 0.25) + + public static let featureLarge = TextStyle(rawValue: "featureLarge", + fontFace: .dsLight, + pointSize: UIDevice.isIPad ? VDSTypography.fontSizeFeature128 : VDSTypography.fontSizeFeature80, + lineHeight: UIDevice.isIPad ? VDSTypography.lineHeightFeature120 : VDSTypography.lineHeightFeature76, + letterSpacing: 0.25) + + public static let boldFeatureLarge = TextStyle(rawValue: "boldFeatureLarge", + fontFace: .dsBold, + pointSize: UIDevice.isIPad ? VDSTypography.fontSizeFeature128 : VDSTypography.fontSizeFeature80, + lineHeight: UIDevice.isIPad ? VDSTypography.lineHeightFeature120 : VDSTypography.lineHeightFeature76, + letterSpacing: 0.25) + + public static let featureMedium = TextStyle(rawValue: "featureMedium", + fontFace: .dsLight, + pointSize: UIDevice.isIPad ? VDSTypography.fontSizeFeature96 : VDSTypography.fontSizeFeature64, + lineHeight: UIDevice.isIPad ? VDSTypography.lineHeightFeature88 : VDSTypography.lineHeightFeature64, + letterSpacing: 0.25) + + public static let boldFeatureMedium = TextStyle(rawValue: "boldFeatureMedium", + fontFace: .dsBold, + pointSize: UIDevice.isIPad ? VDSTypography.fontSizeFeature96 : VDSTypography.fontSizeFeature64, + lineHeight: UIDevice.isIPad ? VDSTypography.lineHeightFeature88 : VDSTypography.lineHeightFeature64, + letterSpacing: 0.25) + + public static let featureSmall = TextStyle(rawValue: "featureSmall", + fontFace: .dsLight, + pointSize: UIDevice.isIPad ? VDSTypography.fontSizeFeature80 : VDSTypography.fontSizeFeature48, + lineHeight: UIDevice.isIPad ? VDSTypography.lineHeightFeature76 : VDSTypography.lineHeightFeature48, + letterSpacing: 0.25) + + public static let boldFeatureSmall = TextStyle(rawValue: "boldFeatureSmall", + fontFace: .dsBold, + pointSize: UIDevice.isIPad ? VDSTypography.fontSizeFeature80 : VDSTypography.fontSizeFeature48, + lineHeight: UIDevice.isIPad ? VDSTypography.lineHeightFeature76 : VDSTypography.lineHeightFeature48, + letterSpacing: 0.25) + + public static let featureXSmall = TextStyle(rawValue: "featureXSmall", + fontFace: .dsLight, + pointSize: UIDevice.isIPad ? VDSTypography.fontSizeFeature64 : VDSTypography.fontSizeFeature40, + lineHeight: UIDevice.isIPad ? VDSTypography.lineHeightFeature64 : VDSTypography.lineHeightFeature40, + letterSpacing: 0.25) + + public static let boldFeatureXSmall = TextStyle(rawValue: "boldFeatureXSmall", + fontFace: .dsBold, + pointSize: UIDevice.isIPad ? VDSTypography.fontSizeFeature64 : VDSTypography.fontSizeFeature40, + lineHeight: UIDevice.isIPad ? VDSTypography.lineHeightFeature64 : VDSTypography.lineHeightFeature40, + letterSpacing: 0.25) + + public static let title2XLarge = TextStyle(rawValue: "title2XLarge", + fontFace: .dsLight, + pointSize: UIDevice.isIPad ? VDSTypography.fontSizeTitle64 : VDSTypography.fontSizeTitle40, + lineHeight: UIDevice.isIPad ? VDSTypography.lineHeightTitle64 : VDSTypography.lineHeightTitle40, + letterSpacing: 0.25) + + public static let boldTitle2XLarge = TextStyle(rawValue: "boldTitle2XLarge", + fontFace: .dsBold, + pointSize: UIDevice.isIPad ? VDSTypography.fontSizeTitle64 : VDSTypography.fontSizeTitle40, + lineHeight: UIDevice.isIPad ? VDSTypography.lineHeightTitle64 : VDSTypography.lineHeightTitle40, + letterSpacing: 0.25) + + public static let titleXLarge = TextStyle(rawValue: "titleXLarge", + fontFace: .dsLight, + pointSize: UIDevice.isIPad ? VDSTypography.fontSizeTitle48 : VDSTypography.fontSizeTitle32, + lineHeight: UIDevice.isIPad ? VDSTypography.lineHeightTitle48 : VDSTypography.lineHeightTitle36, + letterSpacing: 0.25) + + public static let boldTitleXLarge = TextStyle(rawValue: "boldTitleXLarge", + fontFace: .dsBold, + pointSize: UIDevice.isIPad ? VDSTypography.fontSizeTitle48 : VDSTypography.fontSizeTitle32, + lineHeight: UIDevice.isIPad ? VDSTypography.lineHeightTitle48 : VDSTypography.lineHeightTitle36, + letterSpacing: 0.25) + + public static let titleLarge = TextStyle(rawValue: "titleLarge", + fontFace: .dsLight, + pointSize: UIDevice.isIPad ? VDSTypography.fontSizeTitle32 : VDSTypography.fontSizeTitle24, + lineHeight: UIDevice.isIPad ? VDSTypography.lineHeightTitle36 : VDSTypography.lineHeightTitle28, + letterSpacing: 0.25) + + public static let boldTitleLarge = TextStyle(rawValue: "boldTitleLarge", + fontFace: .dsBold, + pointSize: UIDevice.isIPad ? VDSTypography.fontSizeTitle32 : VDSTypography.fontSizeTitle24, + lineHeight: UIDevice.isIPad ? VDSTypography.lineHeightTitle36 : VDSTypography.lineHeightTitle28, + letterSpacing: 0.25) + + public static let titleMedium = TextStyle(rawValue: "titleMedium", + fontFace: .dsLight, + pointSize: UIDevice.isIPad ? VDSTypography.fontSizeTitle24 : VDSTypography.fontSizeTitle20, + lineHeight: UIDevice.isIPad ? VDSTypography.lineHeightTitle28 : VDSTypography.lineHeightTitle24, + letterSpacing: 0.25) + + public static let boldTitleMedium = TextStyle(rawValue: "boldTitleMedium", + fontFace: .dsBold, + pointSize: UIDevice.isIPad ? VDSTypography.fontSizeTitle24 : VDSTypography.fontSizeTitle20, + lineHeight: UIDevice.isIPad ? VDSTypography.lineHeightTitle28 : VDSTypography.lineHeightTitle24, + letterSpacing: 0.25) + + public static let titleSmall = TextStyle(rawValue: "titleSmall", + fontFace: .dsLight, + pointSize: UIDevice.isIPad ? VDSTypography.fontSizeTitle20 : VDSTypography.fontSizeTitle16, + lineHeight: UIDevice.isIPad ? VDSTypography.lineHeightTitle24 : VDSTypography.lineHeightTitle20, + letterSpacing: 0.25) + + public static let boldTitleSmall = TextStyle(rawValue: "boldTitleSmall", + fontFace: .dsBold, + pointSize: UIDevice.isIPad ? VDSTypography.fontSizeTitle20 : VDSTypography.fontSizeTitle16, + lineHeight: UIDevice.isIPad ? VDSTypography.lineHeightTitle24 : VDSTypography.lineHeightTitle20, + letterSpacing: 0.25) + + public static let bodyLarge = TextStyle(rawValue: "bodyLarge", + fontFace: .dsRegular, + pointSize: VDSTypography.fontSizeBody16, + lineHeight: VDSTypography.lineHeightBody20, + letterSpacing: 0.5) + + public static let boldBodyLarge = TextStyle(rawValue: "boldBodyLarge", + fontFace: .dsBold, + pointSize: VDSTypography.fontSizeBody16, + lineHeight: VDSTypography.lineHeightBody20, + letterSpacing: 0.5) + + public static let bodyMedium = TextStyle(rawValue: "bodyMedium", + fontFace: .dsRegular, + pointSize: VDSTypography.fontSizeBody14, + lineHeight: VDSTypography.lineHeightBody18, + letterSpacing: 0.5) + + public static let boldBodyMedium = TextStyle(rawValue: "boldBodyMedium", + fontFace: .dsBold, + pointSize: VDSTypography.fontSizeBody14, + lineHeight: VDSTypography.lineHeightBody18, + letterSpacing: 0.5) + + public static let bodySmall = TextStyle(rawValue: "bodySmall", + fontFace: .dsRegular, + pointSize: VDSTypography.fontSizeBody12, + lineHeight: VDSTypography.lineHeightBody16, + letterSpacing: 0.25) + + public static let boldBodySmall = TextStyle(rawValue: "boldBodySmall", + fontFace: .dsBold, + pointSize: VDSTypography.fontSizeBody12, + lineHeight: VDSTypography.lineHeightBody16, + letterSpacing: 0.5) + + public static let micro = TextStyle(rawValue: "micro", + fontFace: .dsRegular, + pointSize: VDSTypography.fontSizeMicro11, + lineHeight: VDSTypography.lineHeightMicro16, + letterSpacing: 0.25) + + public static let boldMicro = TextStyle(rawValue: "boldMicro", + fontFace: .dsBold, + pointSize: VDSTypography.fontSizeMicro11, + lineHeight: VDSTypography.lineHeightMicro16, + letterSpacing: 0.5) + + public static var allCases: [TextStyle] { + return [ + featureXLarge, + boldFeatureXLarge, + featureLarge, + boldFeatureLarge, + featureMedium, + boldFeatureMedium, + featureSmall, + boldFeatureSmall, + featureXSmall, + boldFeatureXSmall, + title2XLarge, + boldTitle2XLarge, + titleXLarge, + boldTitleXLarge, + titleLarge, + boldTitleLarge, + titleMedium, + boldTitleMedium, + titleSmall, + boldTitleSmall, + bodyLarge, + boldBodyLarge, + bodyMedium, + boldBodyMedium, + bodySmall, + boldBodySmall, + micro, + boldMicro + ] + } + +} + + //MARK: FontCategory extension TextStyle { public enum FontCategory: String, CaseIterable { @@ -91,7 +276,8 @@ extension TextStyle { } else { styleName = "\(rawValue)\(fontSize?.rawValue ?? "")" } - guard let style = TextStyle(rawValue: styleName) else { + + guard let style = TextStyle.textStyle(for: styleName) else { return nil } return style @@ -111,102 +297,6 @@ extension TextStyle { } } -//MARK: PointSize -extension TextStyle { - public var pointSize: CGFloat { - switch self { - case .featureXLarge, .boldFeatureXLarge: - return UIDevice.isIPad ? VDSTypography.fontSizeFeature144 : VDSTypography.fontSizeFeature96 - case .featureLarge, .boldFeatureLarge: - return UIDevice.isIPad ? VDSTypography.fontSizeFeature128 : VDSTypography.fontSizeFeature80 - case .featureMedium, .boldFeatureMedium: - return UIDevice.isIPad ? VDSTypography.fontSizeFeature96 : VDSTypography.fontSizeFeature64 - case .featureSmall, .boldFeatureSmall: - return UIDevice.isIPad ? VDSTypography.fontSizeFeature80 : VDSTypography.fontSizeFeature48 - case .featureXSmall, .boldFeatureXSmall: - return UIDevice.isIPad ? VDSTypography.fontSizeFeature64 : VDSTypography.fontSizeFeature40 - case .title2XLarge, .boldTitle2XLarge: - return UIDevice.isIPad ? VDSTypography.fontSizeTitle64 : VDSTypography.fontSizeTitle40 - case .titleXLarge, .boldTitleXLarge: - return UIDevice.isIPad ? VDSTypography.fontSizeTitle48 : VDSTypography.fontSizeTitle32 - case .titleLarge, .boldTitleLarge: - return UIDevice.isIPad ? VDSTypography.fontSizeTitle32 : VDSTypography.fontSizeTitle24 - case .titleMedium, .boldTitleMedium: - return UIDevice.isIPad ? VDSTypography.fontSizeTitle24 : VDSTypography.fontSizeTitle20 - case .titleSmall, .boldTitleSmall: - return UIDevice.isIPad ? VDSTypography.fontSizeTitle20 : VDSTypography.fontSizeTitle16 - case .bodyLarge, .boldBodyLarge: - return VDSTypography.fontSizeBody16 - case .bodyMedium, .boldBodyMedium: - return VDSTypography.fontSizeBody14 - case .bodySmall, .boldBodySmall: - return VDSTypography.fontSizeBody12 - case .micro, .boldMicro: - return VDSTypography.fontSizeMicro11 - } - } -} - -//MARK: LineHeight -extension TextStyle { - public var lineHeight: CGFloat { - switch self { - case .featureXLarge, .boldFeatureXLarge: - return UIDevice.isIPad ? VDSTypography.lineHeightFeature136 : VDSTypography.lineHeightFeature88 - case .featureLarge, .boldFeatureLarge: - return UIDevice.isIPad ? VDSTypography.lineHeightFeature120 : VDSTypography.lineHeightFeature76 - case .featureMedium, .boldFeatureMedium: - return UIDevice.isIPad ? VDSTypography.lineHeightFeature88 : VDSTypography.lineHeightFeature64 - case .featureSmall, .boldFeatureSmall: - return UIDevice.isIPad ? VDSTypography.lineHeightFeature76 : VDSTypography.lineHeightFeature48 - case .featureXSmall, .boldFeatureXSmall: - return UIDevice.isIPad ? VDSTypography.lineHeightFeature64 : VDSTypography.lineHeightFeature40 - case .title2XLarge, .boldTitle2XLarge: - return UIDevice.isIPad ? VDSTypography.lineHeightTitle64 : VDSTypography.lineHeightTitle40 - case .titleXLarge, .boldTitleXLarge: - return UIDevice.isIPad ? VDSTypography.lineHeightTitle48 : VDSTypography.lineHeightTitle36 - case .titleLarge, .boldTitleLarge: - return UIDevice.isIPad ? VDSTypography.lineHeightTitle36 : VDSTypography.lineHeightTitle28 - case .titleMedium, .boldTitleMedium: - return UIDevice.isIPad ? VDSTypography.lineHeightTitle28 : VDSTypography.lineHeightTitle24 - case .titleSmall, .boldTitleSmall: - return UIDevice.isIPad ? VDSTypography.lineHeightTitle24 : VDSTypography.lineHeightTitle20 - case .bodyLarge, .boldBodyLarge: - return VDSTypography.lineHeightBody20 - case .bodyMedium, .boldBodyMedium: - return VDSTypography.lineHeightBody18 - case .bodySmall, .boldBodySmall: - return VDSTypography.lineHeightBody16 - case .micro, .boldMicro: - return VDSTypography.lineHeightMicro16 - } - } -} - -//MARK: LetterSpacing -extension TextStyle { - public var letterSpacing: CGFloat { - switch self { - case .featureXLarge, - .featureLarge, - .featureMedium, - .featureSmall, - .featureXSmall, - .title2XLarge, - .titleXLarge, - .titleLarge: - return 0.25 - - case .boldBodyLarge, .bodyLarge, - .boldBodyMedium, .bodyMedium: - return 0.5 - - default: - return 0.0 - } - } -} - //MARK: Alignments extension TextStyle { public var aligments: [TextPosition] { @@ -216,55 +306,9 @@ extension TextStyle { //MARK: Fonts extension TextStyle { - public var fontFace: Fonts { - switch self { - case .boldFeatureXLarge, - .boldFeatureLarge, - .boldFeatureMedium, - .boldFeatureSmall, - .boldFeatureXSmall, - .boldTitle2XLarge, - .boldTitleXLarge, - .boldTitleLarge, - .boldTitleMedium, - .boldTitleSmall, - .boldBodyLarge, - .boldBodyMedium: - return .dsBold - - case .featureXLarge, - .featureLarge, - .featureMedium, - .featureSmall, - .featureXSmall, - .title2XLarge, - .titleXLarge: - return .dsLight - - case .titleLarge, - .titleMedium, - .titleSmall, - .bodyLarge, - .bodyMedium: - return .dsRegular - - case .boldBodySmall, - .boldMicro: - return .txBold - - case .bodySmall, - .micro: - return .txRegular - } - } - public var font: UIFont { return fontFace.font(ofSize: pointSize) } - - public var superScriptFont: UIFont { - return fontFace.font(ofSize: pointSize / 2) - } } extension TextStyle { @@ -335,3 +379,12 @@ extension TextStyle: CustomDebugStringConvertible { "Name: \(self.rawValue) FontFace: \(font.fontName) FontWeight: \(self.rawValue.hasPrefix("bold") ? "bold" : "normal") PointSize: \(font.pointSize) LetterSpacing: \(letterSpacing) LineHeight: \(lineHeight)" } } + +extension TextStyle { + public static var defaultStyle: TextStyle { return bodyLarge } + + public static func textStyle(for name: String) -> TextStyle? { + guard let style = TextStyle.allCases.first(where: {$0.rawValue == name }) else { return nil } + return style + } +}