You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Despite Tinamu's suggestion on a previous issue, this problem cannot be solved on SwiftUI view level, nor with using .onReceive modifier, nor with using a custom Binding implementation that filters the input, because these filtering closures run after the paste happened.
Suggested solution
FormatTextField should accept an optional CharacterSet as an init argument.
If given, use this characterset to filter user input at the very first line of public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
The following proof of concept solution seems to fix the problem:
private let allowedInput: CharacterSet = .decimalDigits
public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let string = String(string.unicodeScalars.filter { self.allowedInput.contains($0) })
The text was updated successfully, but these errors were encountered:
Users can input any characters either by using a physical keyboard, or copy pasting into
FormatTextField
.The issue is most prominent when the user is trying to paste something. Here is our example:
user pastes:
12345678 - 12345678 - 12345678
FormatTextField shows:
12345678 - 12345678 - 12
Despite Tinamu's suggestion on a previous issue, this problem cannot be solved on SwiftUI view level, nor with using
.onReceive
modifier, nor with using a customBinding
implementation that filters the input, because these filtering closures run after the paste happened.Suggested solution
FormatTextField
should accept an optionalCharacterSet
as an init argument.If given, use this characterset to filter user input at the very first line of
public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
The following proof of concept solution seems to fix the problem:
The text was updated successfully, but these errors were encountered: