Skip to content

Commit 23d3843

Browse files
authored
Merge pull request #2 from adri567/preference_key_on_pricelabel
Instead of makeView to get the size of the price label, we are now us…
2 parents 220ca54 + faeb2fd commit 23d3843

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
}

Sources/CheesyChart/HelperViews/ChartPriceLabelView.swift

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// ChartPriceLabelView.swift
3-
//
3+
//
44
//
55
// Created by Adrian Suthold on 21.04.22.
66
//
@@ -25,9 +25,15 @@ public struct ChartPriceLabelView: View {
2525
.foregroundColor(setup.chartPriceLabelFontColor)
2626
.background(
2727
GeometryReader { textGeometry in
28-
makeView(geometry: textGeometry)
28+
Rectangle()
29+
.fill(.clear)
30+
.frame(maxWidth: .infinity)
31+
.getTextWidth(textGeometry.size.width)
2932
}
3033
)
34+
.onPreferenceChange(PriceLabelPreferenceKey.self, perform: { value in
35+
textWidth = value
36+
})
3137
.background(setup.chartPriceLabelColor)
3238
.cornerRadius(setup.chartPriceLabelCornerRadius)
3339
.position(
@@ -53,16 +59,6 @@ public struct ChartPriceLabelView: View {
5359
private func checkInput() -> String {
5460
return vm.point > setup.data.count ? "" : setup.data[vm.point].asCurrencyWithTwoDecimals()
5561
}
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-
}
6662
}
6763

6864
//struct PriceLabelView_Previews: PreviewProvider {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
}

0 commit comments

Comments
 (0)