There are a number of interesting responses here and great ideas.
Encountering a situation where I needed to enforce input using a SwiftUI TextField the same way I had done with the textField(_:shouldChangeCharactersIn:replacementString:) delegate method, I found simple filtering works nicely.
@State var textInput: String = ""
...
TextField("numerical entries only", text: $textInput)
.onChange(of: textInput) { newValue in
let numberCharacterSet = "0123456789"
textInput = newValue.filter { numberCharacterSet.contains($0) }
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: