-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathUIFont+Traits.swift
More file actions
58 lines (48 loc) · 2.06 KB
/
Copy pathUIFont+Traits.swift
File metadata and controls
58 lines (48 loc) · 2.06 KB
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
import Foundation
import UIKit
// MARK: - UIFont Traits Helpers
//
extension UIFont {
/// Returns a new instance of the current font with its SymbolicTraits Updated.
///
/// - Parameters:
/// - traits: Traits to be updated
/// - enable: Boolean indicating whether we should insert or remove the specified trait.
///
/// - Returns: A new UIFont with the same descriptors as the current instance, but with its traits updated, as specified.
///
func modifyTraits(_ traits: UIFontDescriptor.SymbolicTraits, enable: Bool) -> UIFont {
let descriptor = fontDescriptor
var newTraits = descriptor.symbolicTraits
if enable {
newTraits.insert(traits)
} else {
newTraits.remove(traits)
}
let familyDescriptor = descriptor.withFamily(familyName)
guard let newDescriptor = familyDescriptor.withSymbolicTraits(newTraits) else {
// A nil descriptor can be returned whenever the requested font cannot be found.
// This is a very viable scenario, and our default handling mechanism for it is to
// return the original, unmodified font.
//
// MARK: Fix oblique type for Chinese
if newTraits.contains(.traitItalic) && familyDescriptor.postscriptName.hasPrefix(".PingFangSC") {
newTraits.remove(.traitItalic)
if let newDescriptor = familyDescriptor.withSymbolicTraits(newTraits) {
return UIFont(descriptor: newDescriptor, size: pointSize)
}
}
return self
}
return UIFont(descriptor: newDescriptor, size: pointSize)
}
/// Returns a boolean indicating if the specified trait is present in the font's descriptor
///
/// - Parameters traits: Traits to be checked.
///
/// - Returns: True if the specified trait was found.
///
func containsTraits(_ traits: UIFontDescriptor.SymbolicTraits) -> Bool {
return fontDescriptor.symbolicTraits.contains(traits)
}
}