Live phone number format using NBAsYouTypeFormatter

I've searched all over the place and I've seen many solutions for this, but most of them are really "hacky" and error prone.

I am trying to format a phone number on a UITextField using the IOS class of the libPhoneNumber NBAsYouTypeFormatter class.

However, I have not been able to find the correct way to use it.

Below is my attempt. (From this thread ios phone number format )

      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 is indeed detecting range.length

, and it does not take into account that if the user selects one or more characters and then type.

If the user does this, it will result in him not being a type.

Has anyone found a more elegant method for this problem?

+3
ios uitextfield swift libphonenumber


source to share


No one has answered this question yet

See similar questions:

4
Ios phone number format

or similar:

6
UITextField How to check if delete key is pressed
3
How to format a live phone number (when using UITextfield) in iOS for different region codes?
2
You can tell if a textbox was selected text
2
Formatting UITextField text as phone number as user types in
2
Can't delete text in UITextField
1
How to restrict UITextField to English only? (Stop typing Chinese pinyin)
1
Create a login screen similar to a lock screen
0
Checking text boxes for entered characters
0
(iOS) How do I get a notification when I press the back button on my keyboard?
0
The user should not delete the given character in a text box that is text



All Articles
Loading...
X
Show
Funny
Dev
Pics