|
| 1 | +// |
| 2 | +// Font.swift |
| 3 | +// IFTTTConnectSDK |
| 4 | +// |
| 5 | +// Created by Pedro Vereza on 07/07/22. |
| 6 | +// Copyright © 2022 IFTTT. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | + |
| 11 | +struct StyleGuide { |
| 12 | + |
| 13 | + struct Font { |
| 14 | + |
| 15 | + /// The weight options available for the app's custom fonts. |
| 16 | + enum CustomFontWeight: String { |
| 17 | + |
| 18 | + /// A font with the heavy weight. |
| 19 | + case heavy = "Heavy" |
| 20 | + |
| 21 | + /// A font with the bold weight. |
| 22 | + case bold = "Bold" |
| 23 | + |
| 24 | + /// A font with the demi bold weight. |
| 25 | + case demiBold = "DemiBold" |
| 26 | + |
| 27 | + /// A font with the medium weight. |
| 28 | + case medium = "Medium" |
| 29 | + } |
| 30 | + |
| 31 | + /// Creates the header 1 font. |
| 32 | + /// |
| 33 | + /// - Parameters: |
| 34 | + /// - weight: The thickness of the font. Defaults to bold. |
| 35 | + /// - isDynamic: Whether the font scales based on a user's settings. Defaults to true. |
| 36 | + /// - isCallout: Whether the font style should be set to `.callout`. Defaults to false. |
| 37 | + /// - Returns: The font configured. |
| 38 | + static func h1(weight: CustomFontWeight = .bold, isDynamic: Bool = true, isCallout: Bool = false) -> UIFont { |
| 39 | + let name = customFontName(withWeight: weight) |
| 40 | + let style: UIFont.TextStyle |
| 41 | + |
| 42 | + if #available(iOS 11, *) { |
| 43 | + style = isCallout == true ? .callout : .largeTitle |
| 44 | + } else { |
| 45 | + style = isCallout == true ? .callout : .title1 |
| 46 | + } |
| 47 | + |
| 48 | + return customFont(withName: name, size: 36, style: style, isDynamic: isDynamic) |
| 49 | + } |
| 50 | + |
| 51 | + /// Creates the header 2 font. |
| 52 | + /// |
| 53 | + /// - Parameters: |
| 54 | + /// - weight: The thickness of the font. Defaults to bold. |
| 55 | + /// - isDynamic: Whether the font scales based on a user's settings. Defaults to true. |
| 56 | + /// - isCallout: Whether the font style should be set to `.callout`. Defaults to false. |
| 57 | + /// - Returns: The font configured. |
| 58 | + static func h2(weight: CustomFontWeight = .bold, isDynamic: Bool = true, isCallout: Bool = false) -> UIFont { |
| 59 | + let name = customFontName(withWeight: weight) |
| 60 | + let style: UIFont.TextStyle = isCallout == true ? .callout : .title1 |
| 61 | + |
| 62 | + return customFont(withName: name, size: 30, style: style, isDynamic: isDynamic) |
| 63 | + } |
| 64 | + |
| 65 | + /// Creates the header 3 font. |
| 66 | + /// |
| 67 | + /// - Parameters: |
| 68 | + /// - weight: The thickness of the font. Defaults to bold. |
| 69 | + /// - isDynamic: Whether the font scales based on a user's settings. Defaults to true. |
| 70 | + /// - isCallout: Whether the font style should be set to `.callout`. Defaults to false. |
| 71 | + /// - Returns: The font configured. |
| 72 | + static func h3(weight: CustomFontWeight = .bold, isDynamic: Bool = true, isCallout: Bool = false) -> UIFont { |
| 73 | + let name = customFontName(withWeight: weight) |
| 74 | + let style: UIFont.TextStyle = isCallout == true ? .callout : .title2 |
| 75 | + |
| 76 | + return customFont(withName: name, size: 28, style: style, isDynamic: isDynamic) |
| 77 | + } |
| 78 | + |
| 79 | + /// Creates the header 4 font. |
| 80 | + /// |
| 81 | + /// - Parameters: |
| 82 | + /// - weight: The thickness of the font. Defaults to bold. |
| 83 | + /// - isDynamic: Whether the font scales based on a user's settings. Defaults to true. |
| 84 | + /// - isCallout: Whether the font style should be set to `.callout`. Defaults to false. |
| 85 | + /// - Returns: The font configured. |
| 86 | + static func h4(weight: CustomFontWeight = .bold, isDynamic: Bool = true, isCallout: Bool = false) -> UIFont { |
| 87 | + let name = customFontName(withWeight: weight) |
| 88 | + let style: UIFont.TextStyle = isCallout == true ? .callout : .title3 |
| 89 | + |
| 90 | + return customFont(withName: name, size: 24, style: style, isDynamic: isDynamic) |
| 91 | + } |
| 92 | + |
| 93 | + /// Creates the header 5 font. |
| 94 | + /// |
| 95 | + /// - Parameters: |
| 96 | + /// - weight: The thickness of the font. Defaults to bold. |
| 97 | + /// - isDynamic: Whether the font scales based on a user's settings. Defaults to true. |
| 98 | + /// - isCallout: Whether the font style should be set to `.callout`. Defaults to false. |
| 99 | + /// - Returns: The font configured. |
| 100 | + static func h5(weight: CustomFontWeight = .bold, isDynamic: Bool = true, isCallout: Bool = false) -> UIFont { |
| 101 | + let name = customFontName(withWeight: weight) |
| 102 | + let style: UIFont.TextStyle = isCallout == true ? .callout : .headline |
| 103 | + |
| 104 | + return customFont(withName: name, size: 20, style: style, isDynamic: isDynamic) |
| 105 | + } |
| 106 | + |
| 107 | + /// Creates the header 6 font. |
| 108 | + /// |
| 109 | + /// - Parameters: |
| 110 | + /// - weight: The thickness of the font. Defaults to bold. |
| 111 | + /// - isDynamic: Whether the font scales based on a user's settings. Defaults to true. |
| 112 | + /// - isCallout: Whether the font style should be set to `.callout`. Defaults to false. |
| 113 | + /// - Returns: The font configured. |
| 114 | + static func h6(weight: CustomFontWeight = .bold, isDynamic: Bool = true, isCallout: Bool = false) -> UIFont { |
| 115 | + let name = customFontName(withWeight: weight) |
| 116 | + let style: UIFont.TextStyle = isCallout == true ? .callout : .subheadline |
| 117 | + |
| 118 | + return customFont(withName: name, size: 18, style: style, isDynamic: isDynamic) |
| 119 | + } |
| 120 | + |
| 121 | + /// Creates the body font. |
| 122 | + /// |
| 123 | + /// - Parameters: |
| 124 | + /// - weight: The thickness of the font. Defaults to medium. |
| 125 | + /// - isDynamic: Whether the font scales based on a user's settings. Defaults to true. |
| 126 | + /// - isCallout: Whether the font style should be set to `.callout`. Defaults to false. |
| 127 | + /// - Returns: The font configured. |
| 128 | + static func body(weight: CustomFontWeight = .medium, isDynamic: Bool = true, isCallout: Bool = false) -> UIFont { |
| 129 | + let name = customFontName(withWeight: weight) |
| 130 | + let style: UIFont.TextStyle = isCallout == true ? .callout : .body |
| 131 | + |
| 132 | + return customFont(withName: name, size: 16, style: style, isDynamic: isDynamic) |
| 133 | + } |
| 134 | + |
| 135 | + /// Creates the standard callout font |
| 136 | + /// This is equivalent to body with demi bold weight and isCallout == true |
| 137 | + /// |
| 138 | + /// - Parameters: |
| 139 | + /// - weight: The thickness of the font. Defaults to demi bold. |
| 140 | + /// - isDynamic: Whether the font scales based on a user's settings. Defaults to true. |
| 141 | + /// - Returns: The font configured. |
| 142 | + static func callout(weight: CustomFontWeight = .demiBold, isDynamic: Bool = true) -> UIFont { |
| 143 | + return Font.body(weight: weight, isDynamic: isDynamic, isCallout: true) |
| 144 | + } |
| 145 | + |
| 146 | + /// Creates the footnote font. |
| 147 | + /// |
| 148 | + /// - Parameters: |
| 149 | + /// - weight: The thickness of the font. Defaults to medium. |
| 150 | + /// - isDynamic: Whether the font scales based on a user's settings. Defaults to true. |
| 151 | + /// - isCallout: Whether the font style should be set to `.callout`. Defaults to false. |
| 152 | + /// - Returns: The font configured. |
| 153 | + static func footnote(weight: CustomFontWeight = .medium, isDynamic: Bool = true, isCallout: Bool = false) -> UIFont { |
| 154 | + let name = customFontName(withWeight: weight) |
| 155 | + let style: UIFont.TextStyle = isCallout == true ? .callout : .footnote |
| 156 | + |
| 157 | + return customFont(withName: name, size: 14, style: style, isDynamic: isDynamic) |
| 158 | + } |
| 159 | + |
| 160 | + /// Creates the caption font. |
| 161 | + /// |
| 162 | + /// - Parameters: |
| 163 | + /// - weight: The thickness of the font. Defaults to medium. |
| 164 | + /// - isDynamic: Whether the font scales based on a user's settings. Defaults to true. |
| 165 | + /// - isCallout: Whether the font style should be set to `.callout`. Defaults to false. |
| 166 | + /// - Returns: The font configured. |
| 167 | + static func caption(weight: CustomFontWeight = .medium, isDynamic: Bool = true, isCallout: Bool = false) -> UIFont { |
| 168 | + let name = customFontName(withWeight: weight) |
| 169 | + let style: UIFont.TextStyle = isCallout == true ? .callout : .caption1 |
| 170 | + |
| 171 | + return customFont(withName: name, size: 12, style: style, isDynamic: isDynamic) |
| 172 | + } |
| 173 | + |
| 174 | + /// Creates the small font. |
| 175 | + /// |
| 176 | + /// - Parameters: |
| 177 | + /// - weight: The thickness of the font. Defaults to bold. |
| 178 | + /// - isDynamic: Whether the font scales based on a user's settings. Defaults to true. |
| 179 | + /// - isCallout: Whether the font style should be set to `.callout`. Defaults to false. |
| 180 | + /// - Returns: The font configured. |
| 181 | + static func small(weight: CustomFontWeight = .bold, isDynamic: Bool = true, isCallout: Bool = false) -> UIFont { |
| 182 | + let name = customFontName(withWeight: weight) |
| 183 | + let style: UIFont.TextStyle = isCallout == true ? .callout : .caption2 |
| 184 | + |
| 185 | + return customFont(withName: name, size: 10, style: style, isDynamic: isDynamic) |
| 186 | + } |
| 187 | + |
| 188 | + private static func customFont(withName name: String, size: CGFloat, style: UIFont.TextStyle, isDynamic: Bool) -> UIFont { |
| 189 | + let font = UIFont(name: name, size: size)! |
| 190 | + |
| 191 | + if #available(iOS 11, *), isDynamic { |
| 192 | + return style.metrics.scaledFont(for: font) |
| 193 | + } |
| 194 | + |
| 195 | + return font |
| 196 | + } |
| 197 | + |
| 198 | + private static func customFontName(withWeight weight: CustomFontWeight) -> String { |
| 199 | + return "AvenirNext-\(weight.rawValue)" |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments