Skip to content

Commit 23eb959

Browse files
authored
Moves font definitions to StyleGuide.Font instead of an extension on UIFont (#294)
1 parent 36a56ed commit 23eb959

6 files changed

Lines changed: 214 additions & 211 deletions

File tree

IFTTT SDK.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
1DFE020D2190E3DB00856ABC /* API.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1DFE020C2190E3DB00856ABC /* API.swift */; };
3030
1DFE02132190E4D300856ABC /* URLRequest+CommonValues.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1DFE02122190E4D300856ABC /* URLRequest+CommonValues.swift */; };
3131
1DFE02172190E58700856ABC /* URLSession+JSONTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1DFE02162190E58700856ABC /* URLSession+JSONTask.swift */; };
32+
6FD1A492287763AD0075626D /* Font.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FD1A491287763AD0075626D /* Font.swift */; };
3233
B556032625A851EE00A29A01 /* Localizable_fr.strings in Resources */ = {isa = PBXBuildFile; fileRef = B556030E25A851ED00A29A01 /* Localizable_fr.strings */; };
3334
B556032725A851EE00A29A01 /* Localizable_en-GB.strings in Resources */ = {isa = PBXBuildFile; fileRef = B556030F25A851ED00A29A01 /* Localizable_en-GB.strings */; };
3435
B556032825A851EE00A29A01 /* Localizable_pt-BR.strings in Resources */ = {isa = PBXBuildFile; fileRef = B556031025A851ED00A29A01 /* Localizable_pt-BR.strings */; };
@@ -118,7 +119,6 @@
118119
DEF4A4922587B9EB00735E98 /* Array+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEF4A4912587B9EB00735E98 /* Array+Helpers.swift */; };
119120
DEF4A4972587BA1A00735E98 /* ArrayHelpersTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEF4A4962587BA1A00735E98 /* ArrayHelpersTests.swift */; };
120121
FC04FE1B21501A8F008AC8A5 /* AboutViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC04FE1A21501A8F008AC8A5 /* AboutViewController.swift */; };
121-
FC094BA5215ED6F5008B35F6 /* UIFont+CommonValues.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC094BA4215ED6F5008B35F6 /* UIFont+CommonValues.swift */; };
122122
FC094BA7215ED72F008B35F6 /* Color.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC094BA6215ED72F008B35F6 /* Color.swift */; };
123123
FC22B19421ACA54000738023 /* ImageCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC22B19321ACA54000738023 /* ImageCache.swift */; };
124124
FC22B19621ACA54A00738023 /* ImageDownloader.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC22B19521ACA54A00738023 /* ImageDownloader.swift */; };
@@ -210,6 +210,7 @@
210210
1DFE020C2190E3DB00856ABC /* API.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = API.swift; sourceTree = "<group>"; };
211211
1DFE02122190E4D300856ABC /* URLRequest+CommonValues.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "URLRequest+CommonValues.swift"; sourceTree = "<group>"; };
212212
1DFE02162190E58700856ABC /* URLSession+JSONTask.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "URLSession+JSONTask.swift"; sourceTree = "<group>"; };
213+
6FD1A491287763AD0075626D /* Font.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Font.swift; sourceTree = "<group>"; };
213214
B545250525A84C20004E2BA8 /* Package.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Package.swift; sourceTree = "<group>"; };
214215
B556030E25A851ED00A29A01 /* Localizable_fr.strings */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = Localizable_fr.strings; path = Resources/Localizable_fr.strings; sourceTree = "<group>"; };
215216
B556030F25A851ED00A29A01 /* Localizable_en-GB.strings */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = "Localizable_en-GB.strings"; path = "Resources/Localizable_en-GB.strings"; sourceTree = "<group>"; };
@@ -302,7 +303,6 @@
302303
DEF4A4912587B9EB00735E98 /* Array+Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Array+Helpers.swift"; sourceTree = "<group>"; };
303304
DEF4A4962587BA1A00735E98 /* ArrayHelpersTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArrayHelpersTests.swift; sourceTree = "<group>"; };
304305
FC04FE1A21501A8F008AC8A5 /* AboutViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AboutViewController.swift; sourceTree = "<group>"; };
305-
FC094BA4215ED6F5008B35F6 /* UIFont+CommonValues.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIFont+CommonValues.swift"; sourceTree = "<group>"; };
306306
FC094BA6215ED72F008B35F6 /* Color.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Color.swift; sourceTree = "<group>"; };
307307
FC1F491322710B62008A1B21 /* Connection_ParsingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Connection_ParsingTests.swift; sourceTree = "<group>"; };
308308
FC1F491522710BE4008A1B21 /* fetch_connection_response.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = fetch_connection_response.json; sourceTree = "<group>"; };
@@ -631,7 +631,7 @@
631631
FC84B84E21C437D500BAF7ED /* Models */,
632632
DE4A4C5C24363284004082BF /* Monitoring */,
633633
FC84B84D21C437B400BAF7ED /* Views */,
634-
FC094BA4215ED6F5008B35F6 /* UIFont+CommonValues.swift */,
634+
6FD1A491287763AD0075626D /* Font.swift */,
635635
FC094BA6215ED72F008B35F6 /* Color.swift */,
636636
FCC6DEB1215D90DD008C8405 /* Assets.swift */,
637637
FC44F54321B75BF3005DA954 /* LocalizedStrings.swift */,
@@ -989,6 +989,7 @@
989989
FCF0C4E421B0A97000DEB117 /* ConnectionNetworkError.swift in Sources */,
990990
DE1F037226A8AD2600B6CF1A /* AuthenticationSessionPresentationContextProvider.swift in Sources */,
991991
1DFE02052190E25500856ABC /* ConnectionCredentialProvider.swift in Sources */,
992+
6FD1A492287763AD0075626D /* Font.swift in Sources */,
992993
DE328DC8243E2FC300603EAC /* SynchronizationTriggerEvent.swift in Sources */,
993994
FC094BA7215ED72F008B35F6 /* Color.swift in Sources */,
994995
DEC08FFC242BAB25007D7039 /* Bundle+Helpers.swift in Sources */,
@@ -1015,7 +1016,6 @@
10151016
FC28D7E52225B1E900D84A1B /* ServiceIconsNetworkController.swift in Sources */,
10161017
DE25265623D8C49D0019C9CB /* Analytics.swift in Sources */,
10171018
FC44F54421B75BF3005DA954 /* LocalizedStrings.swift in Sources */,
1018-
FC094BA5215ED6F5008B35F6 /* UIFont+CommonValues.swift in Sources */,
10191019
1DFE020D2190E3DB00856ABC /* API.swift in Sources */,
10201020
FCBA16052138A4EE00EB1BC8 /* Connection.swift in Sources */,
10211021
DE1F036D26A8A29800B6CF1A /* SignInWithAppleAuthentication.swift in Sources */,

IFTTT SDK/AboutViewController.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ class AboutViewController: UIViewController {
8686
let iftttRemoved = rawText.replacingOccurrences(of: "IFTTT", with: "")
8787

8888
let text = NSMutableAttributedString(string: iftttRemoved,
89-
attributes: [.font : UIFont.h3(weight: .demiBold)])
89+
attributes: [.font : StyleGuide.Font.h3(weight: .demiBold)])
9090
let ifttt = NSAttributedString(string: "IFTTT",
91-
attributes: [.font : UIFont.h3(weight: .heavy)])
91+
attributes: [.font : StyleGuide.Font.h3(weight: .heavy)])
9292
text.insert(ifttt, at: 0)
9393
return text
9494
}
@@ -97,7 +97,7 @@ class AboutViewController: UIViewController {
9797
static var legalTermsText: NSAttributedString {
9898
return LegalTermsText.string(withPrefix: "about.legal.full".localized,
9999
attributes: [.foregroundColor : Color.mediumGrey,
100-
.font : UIFont.body(weight: .demiBold)])
100+
.font : StyleGuide.Font.body(weight: .demiBold)])
101101
}
102102
}
103103
}
@@ -191,7 +191,7 @@ class AboutViewController: UIViewController {
191191
private lazy var deepLinkConnectionButton = PillButton("about.connection-deep-link".localized) {
192192
$0.backgroundColor = Constants.Color.transparentWhite
193193
$0.label.textColor = .white
194-
$0.label.font = .h5()
194+
$0.label.font = StyleGuide.Font.h5()
195195
}
196196

197197
private lazy var downloadOnAppStoreButton: UIButton = {
@@ -369,7 +369,7 @@ private extension AboutViewController {
369369
iconView.constrain.square(length: Constants.Layout.valuePropIconSise)
370370

371371
let label = UILabel(text) {
372-
$0.font = .body(weight: .demiBold)
372+
$0.font = StyleGuide.Font.body(weight: .demiBold)
373373
$0.textColor = .white
374374
$0.textAlignment = .left
375375
$0.numberOfLines = 0

IFTTT SDK/ConnectButton.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public class ConnectButton: UIView {
265265
switchControl.isOn = false
266266
switchControl.knob.backgroundColor = Color.blue
267267
primaryLabelAnimator.configure(.text("Connect"), insets: .avoidLeftKnob)
268-
let initialFooterText = NSMutableAttributedString(string: "Powered by IFTTT", attributes: [.font : UIFont.footnote(weight: .bold)])
268+
let initialFooterText = NSMutableAttributedString(string: "Powered by IFTTT", attributes: [.font : StyleGuide.Font.footnote(weight: .bold)])
269269
footerLabelAnimator.configure(.attributed(initialFooterText))
270270
}
271271

IFTTT SDK/ConnectButtonController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,11 @@ public class ConnectButtonController {
407407
static let errorTextColor = UIColor.red
408408

409409
static var footnoteFont: UIFont {
410-
return .footnote(weight: .demiBold)
410+
return StyleGuide.Font.footnote(weight: .demiBold)
411411
}
412412

413413
static var iftttWordmarkFont: UIFont {
414-
return .footnote(weight: .heavy)
414+
return StyleGuide.Font.footnote(weight: .heavy)
415415
}
416416
}
417417

IFTTT SDK/Font.swift

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
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

Comments
 (0)