-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathScaledFrame.swift
More file actions
46 lines (38 loc) · 1.18 KB
/
ScaledFrame.swift
File metadata and controls
46 lines (38 loc) · 1.18 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
import SwiftUI
extension View {
public func scaledFrame(width: CGFloat? = nil, height: CGFloat? = nil, alignment: Alignment = .center) -> some View {
ScaledFrameView(self, width: width, height: height, alignment: alignment)
}
}
struct ScaledFrameView<Content: View>: View {
let content: Content
let width: CGFloat?
let height: CGFloat?
let alignment: Alignment
@StateObject private var fontScaleManager = FontScaleManager.shared
var fontScale: Double {
fontScaleManager.currentScale
}
var scaledWidth: CGFloat? {
guard let width else {
return nil
}
return width * fontScale
}
var scaledHeight: CGFloat? {
guard let height else {
return nil
}
return height * fontScale
}
init(_ content: Content, width: CGFloat? = nil, height: CGFloat? = nil, alignment: Alignment = .center) {
self.content = content
self.width = width
self.height = height
self.alignment = alignment
}
var body: some View {
content
.frame(width: scaledWidth, height: scaledHeight, alignment: alignment)
}
}