forked from microsoft/fluentui-apple
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fonts.swift
90 lines (84 loc) · 3.02 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
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
import UIKit
@objc(MSFFonts)
public final class Fonts: NSObject {
/// Bold 34pt
@objc public static var largeTitle: UIFont { return preferredFont(forTextStyle: .largeTitle, weight: .bold) }
/// Bold 28 pt
@objc public static var title1: UIFont { return preferredFont(forTextStyle: .title1, 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) }
/// Regular 17pt
@objc public static var body: UIFont { return .preferredFont(forTextStyle: .body) }
/// Regular 15pt
@objc public static var subhead: UIFont { return .preferredFont(forTextStyle: .subheadline) }
/// Regular 13pt
@objc public static var footnote: UIFont { return .preferredFont(forTextStyle: .footnote) }
/// 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) }
/// 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(MSFTextStyle)
public enum TextStyle: Int, CaseIterable {
case largeTitle
case title1
case title2
case headline
case body
case subhead
case footnote
case button1
case button2
case caption1
case caption2
public var font: UIFont {
switch self {
case .largeTitle:
return Fonts.largeTitle
case .title1:
return Fonts.title1
case .title2:
return Fonts.title2
case .headline:
return Fonts.headline
case .body:
return Fonts.body
case .subhead:
return Fonts.subhead
case .footnote:
return Fonts.footnote
case .button1:
return Fonts.button1
case .button2:
return Fonts.button2
case .caption1:
return Fonts.caption1
case .caption2:
return Fonts.caption2
}
}
}