This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
/
Fonts.swift
107 lines (101 loc) · 4.23 KB
/
Fonts.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
import UIKit
public final class MSFonts: NSObject {
/// Bold 30pt - Does not scale automatically with Dynamic Type
@objc public static let largeTitle = UIFont.systemFont(ofSize: 30, weight: .bold)
/// Bold 26pt - Does not scale automatically with Dynamic Type
@objc public static let title1 = UIFont.systemFont(ofSize: 26, weight: .bold)
/// Semibold 22pt
@objc public static var title2: UIFont { return preferredFont(forTextStyle: .title2, weight: .semibold) }
/// Semibold 17pt
@objc public static var headline: UIFont { return .preferredFont(forTextStyle: .headline) }
@objc public static var headlineUnscaled: UIFont { return .preferredFont(forTextStyle: .headline, compatibleWith: UITraitCollection(preferredContentSizeCategory: .large)) }
/// Regular 17pt
@objc public static var body: UIFont { return .preferredFont(forTextStyle: .body) }
@objc public static var bodyUnscaled: UIFont { return .preferredFont(forTextStyle: .body, compatibleWith: UITraitCollection(preferredContentSizeCategory: .large)) }
/// Regular 15pt
@objc public static var subhead: UIFont { return .preferredFont(forTextStyle: .subheadline) }
/// Regular 13pt
@objc public static var footnote: UIFont { return .preferredFont(forTextStyle: .footnote) }
@objc public static var footnoteUnscaled: UIFont { return .preferredFont(forTextStyle: .footnote, compatibleWith: UITraitCollection(preferredContentSizeCategory: .large)) }
/// Medium 15pt
@objc public static var button1: UIFont { return preferredFont(forTextStyle: .subheadline, weight: .medium) }
/// Medium 13pt
@objc public static var button2: UIFont { return preferredFont(forTextStyle: .footnote, weight: .medium) }
/// Medium 10pt - Does not scale automatically with Dynamic Type
@objc public static let button3 = UIFont.systemFont(ofSize: 10, weight: .medium)
/// Medium 15pt - Does not scale automatically with Dynamic Type
@objc public static let button4 = UIFont.systemFont(ofSize: 15, weight: .medium)
/// Regular 12pt
@objc public static var caption1: UIFont { return .preferredFont(forTextStyle: .caption1) }
/// Regular 11pt
@objc public static var caption2: UIFont { return .preferredFont(forTextStyle: .caption2) }
static func preferredFont(forTextStyle style: UIFont.TextStyle, weight: UIFont.Weight, size: CGFloat? = 0) -> UIFont {
let descriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: style)
let newDescriptor = descriptor.addingAttributes([.traits: [UIFontDescriptor.TraitKey.weight: weight]])
let pointSize: CGFloat
if let size = size, size > 0 {
pointSize = size
} else {
pointSize = descriptor.pointSize
}
return UIFont(descriptor: newDescriptor, size: pointSize)
}
private override init() {
super.init()
}
}
@objc public enum MSTextStyle: Int, CaseIterable {
case largeTitle
case title1
case title2
case headline
case headlineUnscaled
case body
case bodyUnscaled
case subhead
case footnote
case footnoteUnscaled
case button1
case button2
case button3
case caption1
case caption2
public var font: UIFont {
switch self {
case .largeTitle:
return MSFonts.largeTitle
case .title1:
return MSFonts.title1
case .title2:
return MSFonts.title2
case .headline:
return MSFonts.headline
case .headlineUnscaled:
return MSFonts.headlineUnscaled
case .body:
return MSFonts.body
case .bodyUnscaled:
return MSFonts.bodyUnscaled
case .subhead:
return MSFonts.subhead
case .footnote:
return MSFonts.footnote
case .footnoteUnscaled:
return MSFonts.footnoteUnscaled
case .button1:
return MSFonts.button1
case .button2:
return MSFonts.button2
case .button3:
return MSFonts.button3
case .caption1:
return MSFonts.caption1
case .caption2:
return MSFonts.caption2
}
}
}