UITextField for Phone Number

2020-01-24 10:54发布

I was wondering how I can format the textField that I'm using for a phone number (ie like the "Add New Contact" page on the iPhone. When I enter in a new mobile phone, ex. 1236890987 it formats it as (123) 689-0987.) I already have the keyboard set as the number pad.

24条回答
混吃等死
2楼-- · 2020-01-24 11:10

Swift 4 (and without NSString)

for format +X(XXX)XXX-XXXX or +X(XXX)XXX-XX-XX Updated and slightly

class ViewController: UIViewController, UITextFieldDelegate {

    var myPhoneNumber = String()

    @IBOutlet weak var phoneTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        phoneTextField.delegate = self
        phoneTextField.keyboardType = .phonePad
    }

    func textFieldDidBeginEditing(_ textField: UITextField) {
        if (textField == self.phoneTextField) && textField.text == ""{
            textField.text = "+7(" //your country code default
        }
    }

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

        if textField == phoneTextField {
            let res = phoneMask(phoneTextField: phoneTextField, textField: textField, range, string)
            myPhoneNumber = res.phoneNumber != "" ? "+\(res.phoneNumber)" : ""
            print("Phone - \(myPhoneNumber)  MaskPhone=\(res.maskPhoneNumber)")
            if (res.phoneNumber.count == 11) || (res.phoneNumber.count == 0) {
                //phone number entered or completely cleared
                print("EDIT END: Phone = \(myPhoneNumber)  MaskPhone = \(res.maskPhoneNumber)")
            }
            return res.result
        }
        return true
    }
}

extension UITextFieldDelegate {
    func phoneMask(phoneTextField: UITextField, textField: UITextField, _ range: NSRange, _ string: String) -> (result: Bool, phoneNumber: String, maskPhoneNumber: String) {
        let oldString = textField.text!
        let newString = oldString.replacingCharacters(in: Range(range, in: oldString)!, with: string)
        //in numString only Numeric characters
        let components = newString.components(separatedBy: CharacterSet.decimalDigits.inverted)
        let numString = components.joined(separator: "")

        let length = numString.count
        let maxCharInPhone = 11

        if newString.count < oldString.count { //backspace to work
            if newString.count <= 2 { //if now "+7(" and push backspace
                phoneTextField.text = ""
                return (false, "", "")
            } else {
                return (true, numString, newString) //will not in the process backspace
            }
        }

        if length > maxCharInPhone { // input is complete, do not add characters
            return (false, numString, newString)
        }
        var indexStart, indexEnd: String.Index
        var maskString = "", template = ""
        var endOffset = 0

        if newString == "+" { // allow add "+" if first Char
            maskString += "+"
        }
        //format +X(XXX)XXX-XXXX
        if length > 0 {
            maskString += "+"
            indexStart = numString.index(numString.startIndex, offsetBy: 0)
            indexEnd = numString.index(numString.startIndex, offsetBy: 1)
            maskString += String(numString[indexStart..<indexEnd]) + "("
        }
        if length > 1 {
            endOffset = 4
            template = ")"
            if length < 4 {
                endOffset = length
                template = ""
            }
            indexStart = numString.index(numString.startIndex, offsetBy: 1)
            indexEnd = numString.index(numString.startIndex, offsetBy: endOffset)
            maskString += String(numString[indexStart..<indexEnd]) + template
        }
        if length > 4 {
            endOffset = 7
            template = "-"
            if length < 7 {
                endOffset = length
                template = ""
            }
            indexStart = numString.index(numString.startIndex, offsetBy: 4)
            indexEnd = numString.index(numString.startIndex, offsetBy: endOffset)
            maskString += String(numString[indexStart..<indexEnd]) + template
        }
        var nIndex: Int; nIndex = 7
//            //format +X(XXX)XXX-XX-XX  -> if need uncoment
//            nIndex = 9
//
//            if length > 7 {
//                endOffset = 9
//                template = "-"
//                if length < 9 {
//                    endOffset = length
//                    template = ""
//                }
//                indexStart = numString.index(numString.startIndex, offsetBy: 7)
//                indexEnd = numString.index(numString.startIndex, offsetBy: endOffset)
//                maskString += String(numString[indexStart..<indexEnd]) + template
//            }
        if length > nIndex {
            indexStart = numString.index(numString.startIndex, offsetBy: nIndex)
            indexEnd = numString.index(numString.startIndex, offsetBy: length)
            maskString += String(numString[indexStart..<indexEnd])
        }
        phoneTextField.text = maskString
        if length == maxCharInPhone {
            //dimiss kayboard
            phoneTextField.endEditing(true)
            return (false, numString, newString)
        }
        return (false, numString, newString)
    }
}
查看更多
Lonely孤独者°
3楼-- · 2020-01-24 11:10

I use this format X (XXX) XXX XX XX it is work in Turkey,

I use it with TableView with Swift 4

func formatToPhoneNumber(withPhoneTextField: UITextField, tableTextField: UITextField, range: NSRange, string: String) -> Bool {

    if (tableTextField == withPhoneTextField) {

        let newString = (tableTextField.text! as NSString).replacingCharacters(in: range, with: string)
        let components = newString.components(separatedBy: NSCharacterSet.decimalDigits.inverted)

        let decimalString = components.joined(separator: "") as NSString
        let length = decimalString.length
        let hasLeadingOne = length > 0 && decimalString.character(at: 0) == (1 as unichar)

        if length == 0 || (length > 11 && !hasLeadingOne) || length > 12 {
            let newLength = (tableTextField.text! as NSString).length + (string as NSString).length - range.length as Int

            return (newLength > 11) ? false : true
        }

        var index = 0 as Int
        let formattedString = NSMutableString()

        if hasLeadingOne {
            formattedString.append("1 ")
            index += 1
        }

        if (length - index) > 1{
            let zeroNumber = decimalString.substring(with: NSMakeRange(index, 1))
            formattedString.appendFormat("%@ ", zeroNumber)
            index += 1
        }

        if (length - index) > 3 {
            let areaCode = decimalString.substring(with: NSMakeRange(index, 3))
            formattedString.appendFormat("(%@) ", areaCode)
            index += 3
        }

        if length - index > 3 {
            let prefix = decimalString.substring(with: NSMakeRange(index, 3))
            formattedString.appendFormat("%@ ", prefix)
            index += 3
        }

        if (length - index) > 3{
            let prefix = decimalString.substring(with: NSMakeRange(index, 2))
            formattedString.appendFormat("%@ ", prefix)
            index += 2
        }

        let remainder = decimalString.substring(from: index)
        formattedString.append(remainder)
        tableTextField.text = formattedString as String

        return false
    } else {
        return true
    }
}

and you can call this func in

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String, indexPath: IndexPath) -> Bool {

}

in any indexPath that your text field in it

for example my textfield in indexPath number 1 so the code will be

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String, indexPath: IndexPath) -> Bool {

    if indexPath.row == 1 {

        let phoneTextField = textField

        return formatToPhoneNumber(withPhoneTextField: phoneTextField, tableTextField: textField, range: range, string: string)

    }
}
查看更多
放我归山
4楼-- · 2020-01-24 11:12

Hopefully, what i'm going to say, will be helpfull for new people programming on iOS, as I am. I did what zingle-dingle suggest (thanks a lot!). To help new ones the code plus what i'll list could help you. 1. you have to add the UITextFieldDelegate on the header file. 2. The UITextField should bind the delegate with the view, in my case the UIViewController, which is the header file. 3. the UITextField should be instatiated, it means, yourtextfile.delegate = self, on the ".m" file.

查看更多
Deceive 欺骗
5楼-- · 2020-01-24 11:13

Here is my Swift 2 code from slightly localised from a UK perspective.

It will format:

+11234567890 as +1 (123) 456 7890

+33123456789 as +33 1 23 45 67 89

+441234123456 as +44 1234 123456 (this has been further localised as 01234 123456) because I don't need to see the country code for UK numbers.

Call as follows:

initInternationalPhoneFormats() //this just needs to be done once

var formattedNo = formatInternationalPhoneNo("+11234567890")

If you have any other country codes and formats or improvements to the code please let me know.

Enjoy.

import Cocoa

extension String
{
    //extension from http://stackoverflow.com/questions/24092884/get-nth-character-of-a-string-in-swift-programming-language

    subscript (i: Int) -> Character
    {
        return self[self.startIndex.advancedBy(i)]
    }

}

var phoneNoFormat = [String : String]()
var localCountryCode: String? = "+44"

func initInternationalPhoneFormats() 
{
    if phoneNoFormat.count == 0
    {
         phoneNoFormat["0"] = "+44 #### ######" //local no (UK)
         phoneNoFormat["02"] = "+44 ## #### #####" //local no (UK) London

         phoneNoFormat["+1"] = "+# (###) ###-####" //US and Canada

         phoneNoFormat["+234"] = "+## # ### ####" //Nigeria
         phoneNoFormat["+2348"] = "+## ### ### ####" //Nigeria Mobile

         phoneNoFormat["+31"] = "+## ### ## ## ##" //Netherlands
         phoneNoFormat["+316"] = "+## # ## ## ## ##" //Netherlands Mobile
         phoneNoFormat["+33"] = "+## # ## ## ## ##" //France
         phoneNoFormat["+39"] = "+## ## ########" //Italy
         phoneNoFormat["+392"] = "+## #### #####" //Italy
         phoneNoFormat["+393"] = "+## ### #######" //Italy

         phoneNoFormat["+44"] = "+## #### ######" //United Kingdom
         phoneNoFormat["+442"] = "+## ## #### #####" //United Kingdom London

         phoneNoFormat["+51"] = "+## # ### ####" //Peru
         phoneNoFormat["+519"] = "+## ### ### ###" //Peru Mobile
         phoneNoFormat["+54"] = "+## ### ### ####" //Argentina
         phoneNoFormat["+541"] = "+## ## #### ####" //Argentina
         phoneNoFormat["+549"] = "+## # ### ### ####" //Argentina
         phoneNoFormat["+55"] = "+## (##) ####-####" //Brazil
         phoneNoFormat["+551"] = "+## (##) ####-###" //Brazil Mobile?

         phoneNoFormat["+60"] = "+## # #### ####" //Malaysia
         phoneNoFormat["+6012"] = "+## ## ### ####" //Malaysia Mobile
         phoneNoFormat["+607"] = "+## # ### ####" //Malaysia?
         phoneNoFormat["+61"] = "+## # #### ####" //Australia
         phoneNoFormat["+614"] = "+## ### ### ###" //Australia Mobile
         phoneNoFormat["+62"] = "+## ## #######" //Indonesia
         phoneNoFormat["+628"] = "+## ### ######" //Indonesia Mobile
         phoneNoFormat["+65"] = "+## #### ####" //Singapore

         phoneNoFormat["+90"] = "+## (###) ### ## ##" //Turkey
     }
 }

 func getDiallingCode(phoneNo: String) -> String
 {
     var countryCode = phoneNo
     while countryCode.characters.count > 0 && phoneNoFormat[countryCode] == nil
     {
         countryCode = String(countryCode.characters.dropLast())
     }
     if countryCode == "0"
     {
         return localCountryCode!
     }
     return countryCode
 }


 func formatInternationalPhoneNo(fullPhoneNo: String, localisePhoneNo: Bool = true) -> String
 {
     if fullPhoneNo == ""
     {
         return ""
     }
     initInternationalPhoneFormats()

     let diallingCode = getDiallingCode(fullPhoneNo)

     let localPhoneNo = fullPhoneNo.stringByReplacingOccurrencesOfString(diallingCode, withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)

     var filteredPhoneNo = (localPhoneNo.characters.filter{["0","1","2","3","4","5","6","7","8","9"].contains($0)})
     if filteredPhoneNo[0] == "0"
     {
         filteredPhoneNo.removeFirst()
     }

     let phoneNo:String = diallingCode + String(filteredPhoneNo)

     if let format = phoneNoFormat[diallingCode]
     {
         let formatLength = format.characters.count
         var formattedPhoneNo = [Character]()
         var formatPos = 0
         for char in phoneNo.characters
         {
             while formatPos < formatLength && format[formatPos] != "#" && format[formatPos] != "+"
             {
                 formattedPhoneNo.append(format[formatPos])
                 formatPos++
             }

             if formatPos < formatLength
             {
                 formattedPhoneNo.append(char)
                 formatPos++
             }
             else
             {
                 break
             }
         }
         if localisePhoneNo,
             let localCode = localCountryCode
         {
             return String(formattedPhoneNo).stringByReplacingOccurrencesOfString(localCode + " ", withString: "0", options: NSStringCompareOptions.LiteralSearch, range: nil) //US users need to remove the extra 0
         }
         return String(formattedPhoneNo)
     }
     return String(filteredPhoneNo)
 }
查看更多
家丑人穷心不美
6楼-- · 2020-01-24 11:13

I have a solution for this but it is having some drawback, see if you can modify and use it. By using this you can achieve both restrict phone number to 10 digit and format it as per US format.

#define MAX_LENGTH 10

Implement it in UITextField Delegate method

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{

 NSInteger insertDelta = string.length - range.length;

if (PhoneNumber_txt.text.length + insertDelta > MAX_LENGTH)
{
    return NO; // the new string would be longer than MAX_LENGTH
}
else {

    range.length = 3;
    range.location = 3;

    PhoneNumber_txt.text = [NSString stringWithFormat:@"(%@)%@-%@", [PhoneNumber_txt.text substringToIndex:3], [PhoneNumber_txt.text substringWithRange:range], [PhoneNumber_txt.text substringFromIndex:6]];
    return YES;
}
 }
查看更多
一纸荒年 Trace。
7楼-- · 2020-01-24 11:14

Here is my take of it. Which is close to what Apple does in the Phone and Contacts app (at least when your region is set to U.S., I am not sure if the behavior changes per region).

I was specifically interested in formatting up to 1 (123) 123-1234 and supporting longer numbers with no formatting. There is also a bug in just checking range.length == 1 (for delete/backspace) in the other solutions which prevents a user from selecting the entire string or part of it and pressing the delete/backspace key, this addresses that situation.

There are some strange behaviors that occur when you start selecting a range in the middle and edit, where the cursor always ends up at the end of the string due to setting the text fields value. I am not sure how to reposition the cursor in a UITextField, I presume Apple is actually using a UITextView in the Contacts and Phone apps as they maintain cursor position whilst doing this inline formatting, they seem to handle all the little nuances! I wish they'd just give us this out of the box.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSMutableString *newString = [NSMutableString stringWithString:textField.text];
    [newString replaceCharactersInRange:range withString:string];
    NSString *phoneNumberString = [self formattedPhoneNumber:newString];

    if (range.length >= 1) { // backspace/delete
        if (phoneNumberString.length > 1) {
            // the way we format the number it is possible that when the user presses backspace they are not deleting the last number
            // in the string, so we need to check if the last character is a number, if it isn't we need to delete everything after the
            // last number in the string
            unichar lastChar = [phoneNumberString characterAtIndex:phoneNumberString.length-1];
            NSCharacterSet *numberCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"1234567890#*"];
            if (![numberCharacterSet characterIsMember:lastChar]) {
                NSRange numberRange = [phoneNumberString rangeOfCharacterFromSet:numberCharacterSet options:NSBackwardsSearch];
                phoneNumberString = [phoneNumberString substringToIndex:numberRange.location+1];
            }
        }
    }

    textField.text = phoneNumberString;

    return NO;
}

- (NSString *)formattedPhoneNumber:(NSString *)string {
    NSString *formattedPhoneNumber = @"";
    NSCharacterSet *numberCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"1234567890#*+"];

    NSRange pauseRange = [string rangeOfString:@","];
    NSRange waitRange = [string rangeOfString:@";"];


    NSString *numberStringToFormat = nil;
    NSString *numberStringToAppend = @"";
    if (pauseRange.location != NSNotFound || waitRange.location != NSNotFound) {
        NSString *choppedString = [string substringToIndex:MIN(pauseRange.location, waitRange.location)];
        numberStringToFormat = [[choppedString componentsSeparatedByCharactersInSet:[numberCharacterSet invertedSet]] componentsJoinedByString:@""];
        numberStringToAppend = [string substringFromIndex:MIN(pauseRange.location, waitRange.location)];
    } else {
        numberStringToFormat = [[string componentsSeparatedByCharactersInSet:[numberCharacterSet invertedSet]] componentsJoinedByString:@""];
    }

    if ([numberStringToFormat hasPrefix:@"0"] || [numberStringToFormat hasPrefix:@"11"]) {
        // numbers starting with 0 and 11 should not be formatted
        formattedPhoneNumber = numberStringToFormat;

    } else if ([numberStringToFormat hasPrefix:@"1"]) {
        if (numberStringToFormat.length <= 1) {
            // 1
            formattedPhoneNumber = numberStringToFormat;
        } else if (numberStringToFormat.length <= 4) {
            // 1 (234)
            NSString *areaCode = [numberStringToFormat substringFromIndex:1];
            if (areaCode.length < 3) {
                formattedPhoneNumber = [NSString stringWithFormat:@"1 (%@",
                                        [numberStringToFormat substringFromIndex:1]]; // 1 (XXX)
            } else {
                formattedPhoneNumber = [NSString stringWithFormat:@"1 (%@) ",
                                        [numberStringToFormat substringFromIndex:1]]; // 1 (XXX)
            }

        } else if (numberStringToFormat.length <= 7) {
            // 1 (234) 123
            formattedPhoneNumber = [NSString stringWithFormat:@"1 (%@) %@",
                                    [numberStringToFormat substringWithRange:NSMakeRange(1, 3)], //1 (XXX) 123
                                    [numberStringToFormat substringFromIndex:4]]; // 1 (234) XXX

        } else if (numberStringToFormat.length <= 11) {
            // 1 (123) 123-1234
            formattedPhoneNumber = [NSString stringWithFormat:@"1 (%@) %@-%@",
                                    [numberStringToFormat substringWithRange:NSMakeRange(1, 3)], //1 (XXX) 123
                                    [numberStringToFormat substringWithRange:NSMakeRange(4, 3)], //1 (234) XXX-1234
                                    [numberStringToFormat substringFromIndex:7]]; // 1 (234) 123-XXXX
        } else {
            // 1123456789012....
            formattedPhoneNumber = numberStringToFormat;
        }
    } else {
        if (numberStringToFormat.length <= 3) {
            // 123
            formattedPhoneNumber = numberStringToFormat;
        } else if (numberStringToFormat.length <= 7) {
            // 123-1234
            formattedPhoneNumber = [NSString stringWithFormat:@"%@-%@",
                                    [numberStringToFormat substringToIndex:3], // XXX-1234
                                    [numberStringToFormat substringFromIndex:3]]; // 123-XXXX
        } else if (numberStringToFormat.length <= 10) {
            // (123) 123-1234
            formattedPhoneNumber = [NSString stringWithFormat:@"(%@) %@-%@",
                                    [numberStringToFormat substringToIndex:3], // (XXX) 123-1234
                                    [numberStringToFormat substringWithRange:NSMakeRange(3, 3)], // (123) XXX-1234
                                    [numberStringToFormat substringFromIndex:6]]; // (123) 123-XXXX

        } else {
            // 123456789012....
            formattedPhoneNumber = numberStringToFormat;
        }
    }

    if (numberStringToAppend.length > 0) {
        formattedPhoneNumber = [NSString stringWithFormat:@"%@%@", formattedPhoneNumber, numberStringToAppend];
    }

    return formattedPhoneNumber;
}
查看更多
登录 后发表回答