Skip to content

Instantly share code, notes, and snippets.

@laevandus
Last active August 30, 2020 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laevandus/723bfdd4eb171718b6ec1a11f3d68500 to your computer and use it in GitHub Desktop.
Save laevandus/723bfdd4eb171718b6ec1a11f3d68500 to your computer and use it in GitHub Desktop.
import Combine
import SwiftUI
struct FormatterView: View {
@State var limits: TemperatureLimits
var body: some View {
VStack(spacing: 16) {
VStack {
Text("Low")
NumberTextField("Low", value: $limits.lowNumber, formatter: NumberFormatter.decimal)
}
VStack {
Text("High")
NumberTextField("High", value: $limits.highNumber, formatter: NumberFormatter.decimal)
}
Button(action: {
self.limits.low = Int.random(in: 0...40)
self.limits.high = Int.random(in: 50...100)
}, label: {
Text("Randomise")
})
Text("Current: \(limits.low) - \(limits.high)")
Spacer()
}.padding()
.multilineTextAlignment(.center)
}
}
extension NumberFormatter {
static var decimal: NumberFormatter {
let formatter = NumberFormatter()
formatter.allowsFloats = false
return formatter
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment