Skip to content

Commit

Permalink
Merge pull request #2 from adri567/preference_key_on_pricelabel
Browse files Browse the repository at this point in the history
Instead of makeView to get the size of the price label, we are now us…
  • Loading branch information
adri567 authored May 18, 2022
2 parents 220ca54 + faeb2fd commit 23d3843
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
19 changes: 19 additions & 0 deletions Sources/CheesyChart/Extensions/View+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// View+Extensions.swift
//
//
// Created by Adrian Suthold on 18.05.22.
//

import Foundation
import SwiftUI

extension View {

/// Get the actual width of the price label
/// - Parameter width: Width of the price label
/// - Returns: Since we are using this method as a modifier we need to return some View, to use it on a rectangle
func getTextWidth(_ width: CGFloat) -> some View {
preference(key: PriceLabelPreferenceKey.self, value: width)
}
}
20 changes: 8 additions & 12 deletions Sources/CheesyChart/HelperViews/ChartPriceLabelView.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// ChartPriceLabelView.swift
//
//
//
// Created by Adrian Suthold on 21.04.22.
//
Expand All @@ -25,9 +25,15 @@ public struct ChartPriceLabelView: View {
.foregroundColor(setup.chartPriceLabelFontColor)
.background(
GeometryReader { textGeometry in
makeView(geometry: textGeometry)
Rectangle()
.fill(.clear)
.frame(maxWidth: .infinity)
.getTextWidth(textGeometry.size.width)
}
)
.onPreferenceChange(PriceLabelPreferenceKey.self, perform: { value in
textWidth = value
})
.background(setup.chartPriceLabelColor)
.cornerRadius(setup.chartPriceLabelCornerRadius)
.position(
Expand All @@ -53,16 +59,6 @@ public struct ChartPriceLabelView: View {
private func checkInput() -> String {
return vm.point > setup.data.count ? "" : setup.data[vm.point].asCurrencyWithTwoDecimals()
}

/// 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.
/// - Parameter geometry: Geometry of the Text
/// - Returns: Rectangle View that is in the background of our Text
private func makeView(geometry: GeometryProxy) -> some View {
DispatchQueue.main.async {
self.textWidth = geometry.size.width
}
return Rectangle().fill(.clear)
}
}

//struct PriceLabelView_Previews: PreviewProvider {
Expand Down
19 changes: 19 additions & 0 deletions Sources/CheesyChart/Utilities/PriceLabelPreferenceKey.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// PriceLabelPreferenceKey.swift
//
//
// Created by Adrian Suthold on 18.05.22.
//

import Foundation
import SwiftUI

struct PriceLabelPreferenceKey: PreferenceKey {

static var defaultValue: CGFloat = 0.0

static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
value = nextValue()
}

}

0 comments on commit 23d3843

Please sign in to comment.