File tree Expand file tree Collapse file tree 3 files changed +46
-12
lines changed Expand file tree Collapse file tree 3 files changed +46
-12
lines changed Original file line number Diff line number Diff line change
1
+ //
2
+ // View+Extensions.swift
3
+ //
4
+ //
5
+ // Created by Adrian Suthold on 18.05.22.
6
+ //
7
+
8
+ import Foundation
9
+ import SwiftUI
10
+
11
+ extension View {
12
+
13
+ /// Get the actual width of the price label
14
+ /// - Parameter width: Width of the price label
15
+ /// - Returns: Since we are using this method as a modifier we need to return some View, to use it on a rectangle
16
+ func getTextWidth( _ width: CGFloat ) -> some View {
17
+ preference ( key: PriceLabelPreferenceKey . self, value: width)
18
+ }
19
+ }
Original file line number Diff line number Diff line change 1
1
//
2
2
// ChartPriceLabelView.swift
3
- //
3
+ //
4
4
//
5
5
// Created by Adrian Suthold on 21.04.22.
6
6
//
@@ -25,9 +25,15 @@ public struct ChartPriceLabelView: View {
25
25
. foregroundColor ( setup. chartPriceLabelFontColor)
26
26
. background (
27
27
GeometryReader { textGeometry in
28
- makeView ( geometry: textGeometry)
28
+ Rectangle ( )
29
+ . fill ( . clear)
30
+ . frame ( maxWidth: . infinity)
31
+ . getTextWidth ( textGeometry. size. width)
29
32
}
30
33
)
34
+ . onPreferenceChange ( PriceLabelPreferenceKey . self, perform: { value in
35
+ textWidth = value
36
+ } )
31
37
. background ( setup. chartPriceLabelColor)
32
38
. cornerRadius ( setup. chartPriceLabelCornerRadius)
33
39
. position (
@@ -53,16 +59,6 @@ public struct ChartPriceLabelView: View {
53
59
private func checkInput( ) -> String {
54
60
return vm. point > setup. data. count ? " " : setup. data [ vm. point] . asCurrencyWithTwoDecimals ( )
55
61
}
56
-
57
- /// Gives use the geometry of a text. In this case we are asign the geometry.size.width to our textWidth variable to get the width for further using in the calculateBorder() method.
58
- /// - Parameter geometry: Geometry of the Text
59
- /// - Returns: Rectangle View that is in the background of our Text
60
- private func makeView( geometry: GeometryProxy ) -> some View {
61
- DispatchQueue . main. async {
62
- self . textWidth = geometry. size. width
63
- }
64
- return Rectangle ( ) . fill ( . clear)
65
- }
66
62
}
67
63
68
64
//struct PriceLabelView_Previews: PreviewProvider {
Original file line number Diff line number Diff line change
1
+ //
2
+ // PriceLabelPreferenceKey.swift
3
+ //
4
+ //
5
+ // Created by Adrian Suthold on 18.05.22.
6
+ //
7
+
8
+ import Foundation
9
+ import SwiftUI
10
+
11
+ struct PriceLabelPreferenceKey : PreferenceKey {
12
+
13
+ static var defaultValue : CGFloat = 0.0
14
+
15
+ static func reduce( value: inout CGFloat , nextValue: ( ) -> CGFloat ) {
16
+ value = nextValue ( )
17
+ }
18
+
19
+ }
You can’t perform that action at this time.
0 commit comments