-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFontDesigner.swift
86 lines (67 loc) · 2.58 KB
/
FontDesigner.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
//
// FontDesigner.swift
//
//
// Created by Andre Albach on 14.02.22.
//
import Combine
import SwiftUI
/// The view model for `FontDesignerView`.
final class FontDesigner: ObservableObject {
/// The font color
@Published var fontColor: CGColor = UIColor.label.cgColor
/// The font size
@Published var fontSize: CGFloat = 16
/// The font descriptor, if available
@Published var fontDescriptor: UIFontDescriptor? = nil
// MARK: - UI controling
/// The font name string, displayed by the font picker
@Published private(set) var displayedFontName: String = NSLocalizedString("Font", comment: "")
/// The preview text to preview how the font configuration looks like
@Published private(set) var previewText: AttributedString = AttributedString("Some preview text")
/// Indicator, if the font picker is currently visible
@Published var isFontPickerActive: Bool = false
// MARK: - Other
/// A store for all the subscriptions. So we can react on the changes
private var subscriptions: Set<AnyCancellable> = []
/// Initialisation
init() {
$fontDescriptor
.removeDuplicates()
.receive(on: RunLoop.main)
.sink { [weak self] newValue in
self?.updatePreviewText()
guard let fontName = newValue?.postscriptName else { return }
self?.displayedFontName = String(format: NSLocalizedString("Font: #NAME#", comment: ""), fontName)
}
.store(in: &subscriptions)
$fontSize
.removeDuplicates()
.receive(on: RunLoop.main)
.sink { [weak self] _ in
self?.updatePreviewText()
}
.store(in: &subscriptions)
}
/// This function will update the preview text and use the latest values
private func updatePreviewText() {
var text = AttributedString("Some preview text")
if let fontDescriptor = fontDescriptor {
text.uiKit.font = UIFont(descriptor: fontDescriptor, size: fontSize)
} else {
text.uiKit.font = UIFont.systemFont(ofSize: fontSize)
}
previewText = text
}
}
// MARK: - Preview data
extension FontDesigner {
/// Some preview data
static let preview: FontDesigner = {
let designer = FontDesigner()
designer.fontSize = 25
designer.fontColor = UIColor.red.cgColor
designer.fontDescriptor = UIFontDescriptor(name: "Marker Felt", size: 25)
return designer
}()
}