Last active
April 10, 2023 17:01
-
-
Save daniloc/254db4eb8e009acc2409edc2da2f18ac to your computer and use it in GitHub Desktop.
SwiftUI TextField for decimal input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct DecimalTextField: View { | |
@Binding var decimalValue: Decimal | |
@State var clearValue: Decimal = 1 | |
var clearValueString: String { | |
return String("\(clearValue)") | |
} | |
@State private var lastValidInput: String? | |
let localizedSeparator = NumberFormatter().decimalSeparator! | |
var inputValidator: Binding<String> { | |
//adapted from: https://stackoverflow.com/a/58031510/150181 | |
Binding<String>( | |
get: { self.lastValidInput ?? String("\(self.decimalValue)") }, | |
set: { | |
let matchesInputFormat = $0.range(of: "^[0-9]{0,9}([\(self.localizedSeparator)][0-9]{0,9})?$", options: .regularExpression) != nil | |
//adapted from https://stackoverflow.com/a/54381284/150181 | |
if matchesInputFormat { | |
self.decimalValue = Decimal(string: $0) ?? self.clearValue | |
self.lastValidInput = $0 | |
} else { | |
self.inputValidator.wrappedValue = self.lastValidInput ?? self.clearValueString | |
} | |
}) | |
} | |
var body: some View { | |
HStack { | |
TextField("", text: inputValidator) | |
.keyboardType(.decimalPad) | |
Button(action: { | |
self.inputValidator.wrappedValue = self.clearValueString | |
}) { | |
Image(systemName: "xmark.circle.fill") | |
.accentColor(.secondary) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This may be stupid—it's my first SwiftUI component, so no guarantees. Still, solves the problem of decimal input with user-friendly validation. All you have to do is pass it a Decimal binding: