-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathScaledModifier.swift
More file actions
66 lines (52 loc) · 1.6 KB
/
ScaledModifier.swift
File metadata and controls
66 lines (52 loc) · 1.6 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
59
60
61
62
63
64
65
66
import SwiftUI
// MARK: - padding
public extension View {
func scaledPadding(_ length: CGFloat?) -> some View {
scaledPadding(.all, length)
}
func scaledPadding(_ edges: Edge.Set = .all, _ length: CGFloat? = nil) -> some View {
ScaledPaddingView(self, edges: edges, length: length)
}
}
struct ScaledPaddingView<Content: View>: View {
let content: Content
let edges: Edge.Set
let length: CGFloat?
@StateObject private var fontScaleManager = FontScaleManager.shared
var fontScale: Double {
fontScaleManager.currentScale
}
init(_ content: Content, edges: Edge.Set, length: CGFloat? = nil) {
self.content = content
self.edges = edges
self.length = length
}
var body: some View {
content
.padding(edges, length.map { $0 * fontScale })
}
}
// MARK: - scaleEffect
public extension View {
func scaledScaleEffect(_ s: CGFloat, anchor: UnitPoint = .center) -> some View {
ScaledScaleEffectView(self, s, anchor: anchor)
}
}
struct ScaledScaleEffectView<Content: View>: View {
let content: Content
let s: CGFloat
let anchor: UnitPoint
@StateObject private var fontScaleManager = FontScaleManager.shared
var fontScale: Double {
fontScaleManager.currentScale
}
init(_ content: Content, _ s: CGFloat, anchor: UnitPoint = .center) {
self.content = content
self.s = s
self.anchor = anchor
}
var body: some View {
content
.scaleEffect(s * fontScale, anchor: anchor)
}
}