diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/views/LabelledValue.swift b/ui/BankingiOSApp/BankingiOSApp/ui/views/LabelledValue.swift index 24a6b8d0..dc980d30 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/views/LabelledValue.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/views/LabelledValue.swift @@ -26,44 +26,9 @@ struct LabelledValue: View { Spacer() - if textFitsIntoAvailableSpace { - valueText - } - else { - ScrollView(.horizontal) { - valueText - } - } + TextWithScrollView(value) } } - - private var valueText: some View { - return Text(value) - .detailForegroundColor() - - .background( - - // Render the limited text and measure its size - Text(value).lineLimit(1) - .background(GeometryReader { availableSpace in - - ZStack { - - // Render the text without restrictions and measure its size - Text(self.value) - .background(GeometryReader { requiredSpace in - - // And compare the two - Color.clear.onAppear { - self.textFitsIntoAvailableSpace = self.textFitsIntoAvailableSpace == false ? false : availableSpace.size.width >= requiredSpace.size.width - } - }) - } - .frame(width: .greatestFiniteMagnitude) - }) - .hidden() // Hide the background - ) - } } @@ -75,3 +40,12 @@ struct LabelledValue_Previews: PreviewProvider { } } + + +extension LabelledValue { + + init(_ label: LocalizedStringKey, _ value: String?) { + self.init(label, value ?? "") + } + +} diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/views/TextWithScrollView.swift b/ui/BankingiOSApp/BankingiOSApp/ui/views/TextWithScrollView.swift new file mode 100644 index 00000000..fb56ca20 --- /dev/null +++ b/ui/BankingiOSApp/BankingiOSApp/ui/views/TextWithScrollView.swift @@ -0,0 +1,68 @@ +import SwiftUI + + +struct TextWithScrollView: View { + + private let value: LocalizedStringKey + + @State private var textFitsIntoAvailableSpace = true + + + init( _ value: LocalizedStringKey) { + self.value = value + } + + init(_ value: String) { + self.init(LocalizedStringKey(value)) + } + + + var body: some View { + if textFitsIntoAvailableSpace { + valueText + } + else { + ScrollView(.horizontal) { + valueText + } + } + } + + private var valueText: some View { + return Text(value) + .detailForegroundColor() + + .background( + + // Render the limited text and measure its size + Text(value).lineLimit(1) + .background(GeometryReader { availableSpace in + + ZStack { + + // Render the text without restrictions and measure its size + Text(self.value) + .background(GeometryReader { requiredSpace in + + // And compare the two + Color.clear.onAppear { + self.textFitsIntoAvailableSpace = self.textFitsIntoAvailableSpace == false ? false : availableSpace.size.width >= requiredSpace.size.width + } + }) + } + .frame(width: .greatestFiniteMagnitude) + }) + .hidden() // Hide the background + ) + } + +} + + +struct TextWithScrollView_Previews: PreviewProvider { + + static var previews: some View { + TextWithScrollView("Text") + } + +}