Ios phone number format

In my application, I take the phone number as input from the user. The number must be in US format. I want to show it as (555) -888-888 dynamically. For example, when a user starts entering a number, when he reaches 4 digits, he shows a number like this (555) -4 and so on. I tried using the replaceString method, but found that it would not work.

+4
source share
5 answers

Take a look at NBAsYouTypeFormatterthe libPhoneNumber-iOS library class .

You create a new instance NSAsYouTypeFormatterwith the code for your American region:

NBAsYouTypeFormatter *asYouTypeFormatter = [[NBAsYouTypeFormatter alloc] initWithRegionCode:REGION_CODE_STRING];

Then each time the user changes the phone number that you are calling:

- (NSString*)inputDigit:(NSString*)nextChar;

or

- (NSString*)removeLastDigit;

NSString - .

+6

. , .

libPhoneNumber-iOS . , .

.

(1) , .

#import "NBPhoneMetaDataGenerator.h"
#import "NBPhoneNumberUtil.h"
#import "NBAsYouTypeFormatter.h"

NBAsYouTypeFormatter :

NBAsYouTypeFormatter *asYouTypeFormatter;

(2) viewDidLoad :

asYouTypeFormatter = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"IN"];

: @ "IN" . , . plist, libPhoneNumber-iOS, .

(3) UITextField yout.

#pragma mark
#pragma mark - Phone Number textfield formatting

# define LIMIT 18 // Or whatever you want

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // Just allow 18 digits
    if(!(([string length] + range.location) > LIMIT))
    {
        // Something entered by user
        if(range.length == 0)
        {
            [txtNumber setText:[asYouTypeFormatter inputDigit:string]];
        }

        // Backspace
        else if(range.length == 1)
        {
            [txtNumber setText:[asYouTypeFormatter removeLastDigit]];
        }
    }

    return NO;
}

, !!!

+6

, , , .

tableView, textField. . .

, , Swift.


  • , OfYourProject-Bridging-Header :

    #import "NBAsYouTypeFormatter.h"
    
  • NBAsYouTypeFormatter:

    private var phoneFormatter: NBAsYouTypeFormatter!
    
  • in viewDidLoad hasSet , NBAsYouTypeFormatter :

    // yourRegionCode is a 2-digit country code (ISO 3166)
    phoneFormatter = NBAsYouTypeFormatter(regionCode: yourRegionCode)
    
  • viewController TextFieldDelegate shouldChangeCharactersInRange:

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    
        // Phone number cell
        if cellContainsPhoneNumber { // This is specific to your own tableView
    
            // Formatting phone number as you type
            let textWithoutSpaces = textField.text.stringByReplacingOccurrencesOfString(" ", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
            phoneFormatter.inputString(textWithoutSpaces) // This is the initial value of the phoneFormatter each time the delegate method is called
    
            let formattedNumber: String!
            if string == "" {
                formattedNumber = phoneFormatter.removeLastDigit()
            } else {
                formattedNumber = phoneFormatter.inputDigit(string)
            }
    
            // set the textField text with the new formattedNumber
            textField.text = formattedNumber
            return false 
        }
        return true
    }
    

, , Apple. , .

+3

, (Swift 2.0):

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    // Allow up to 18 chars
    if !(string.characters.count + range.location > 18) {
        if range.length == 0 {
            // original text didn't change
            textField.text = phoneFormatter?.inputDigit(string)
        } else if range.length == 1 { 
            // user pressed backspace
            textField.text = phoneFormatter?.removeLastDigit()
        } else if range.length == textField.text?.characters.count {
            // text was cleared
            phoneFormatter?.clear()
            textField.text = ""
        }
    }
    return false
}

, , , "" " " β†’ "".

There are some edge cases, such as a user editing certain numbers in a phone number that is not processed, but can be easily added.

+3
source

You can use this library to enter formatting during input https://github.com/luximetr/AnyFormatKit

Example

let textInputController = TextInputController()

let textInput = TextInputField() // or TextInputView or any TextInput
textInputController.textInput = textInput // setting textInput

let formatter = TextInputFormatter(textPattern: "### (###) ###-##-##", prefix: "+12")
textInputController.formatter = formatter // setting formatter

In this case, the TextInputController will format the text in textField or textView.

0
source

Source: https://habr.com/ru/post/1529389/


All Articles