I have searched all over the place and I have seen many solutions to this, but most of it are really being "hackish" and error prone.
I am trying to format the phone number on UITextField using the libPhoneNumber iOS Class NBAsYouTypeFormatter.
However, I couldnt find a proper way of using it.
Below is my attempt. (From this thread Phone number format ios)
var asYouTypeFormatter = NBAsYouTypeFormatter(regionCode: "US");
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if textField == phoneTextField {
if range.length == 0 {
textField.text = asYouTypeFormatter.inputDigit(textField.text + string);
}
else if range.length == 1 {
textField.text = asYouTypeFormatter.removeLastDigit();
}
return false;
}
return true;
}
This code really detect range.length
and it doesnt take into account that if the user select one or more character and then type.
If the user does that, it will resulted in it not being type.
Have anyone found a more elegant method to this problem?
Note: I'm writing in objective C, I didn't test the Swift syntax, but the logic of this solution works for me;
This is my Objective-C working code: