web-dev-qa-db-fra.com

Formatage des numéros de téléphone sur iOS

J'ai un champ de texte où l'utilisateur entre des données. C'est un champ de numéro de téléphone. Si l'utilisateur entre 1234567890, Je veux qu'il s'affiche comme (123)-(456)-7890 comme type d'utilisateur. Comment est-ce possible?

64
Moni Viki

Cela vous aidera

Format (xxx) xxx-xxxx

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    int length = (int)[self getLength:textField.text];
    //NSLog(@"Length  =  %d ",length);

    if(length == 10)
    {
        if(range.length == 0)
            return NO;
    }

    if(length == 3)
    {
        NSString *num = [self formatNumber:textField.text];
        textField.text = [NSString stringWithFormat:@"(%@) ",num];

        if(range.length > 0)
            textField.text = [NSString stringWithFormat:@"%@",[num substringToIndex:3]];
    }
    else if(length == 6)
    {
        NSString *num = [self formatNumber:textField.text];
        //NSLog(@"%@",[num  substringToIndex:3]);
        //NSLog(@"%@",[num substringFromIndex:3]);
        textField.text = [NSString stringWithFormat:@"(%@) %@-",[num  substringToIndex:3],[num substringFromIndex:3]];

        if(range.length > 0)
            textField.text = [NSString stringWithFormat:@"(%@) %@",[num substringToIndex:3],[num substringFromIndex:3]];
    }

    return YES;
}

- (NSString *)formatNumber:(NSString *)mobileNumber
{
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];

    NSLog(@"%@", mobileNumber);

    int length = (int)[mobileNumber length];
    if(length > 10)
    {
        mobileNumber = [mobileNumber substringFromIndex: length-10];
        NSLog(@"%@", mobileNumber);

    }

    return mobileNumber;
}

- (int)getLength:(NSString *)mobileNumber
{
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];

    int length = (int)[mobileNumber length];

    return length;
}
113
wan

Cela semblait plus clair et gère la suppression de tous les caractères indésirables de manière beaucoup plus agréable. Formats corrects pour 1 (###) ### - #### ou (###) ### - ####

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    NSArray *components = [newString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
    NSString *decimalString = [components componentsJoinedByString:@""];

    NSUInteger length = decimalString.length;
    BOOL hasLeadingOne = length > 0 && [decimalString characterAtIndex:0] == '1';

    if (length == 0 || (length > 10 && !hasLeadingOne) || (length > 11)) {
        textField.text = decimalString;
        return NO;
    }

    NSUInteger index = 0;
    NSMutableString *formattedString = [NSMutableString string];

    if (hasLeadingOne) {
        [formattedString appendString:@"1 "];
        index += 1;
    }

    if (length - index > 3) {
        NSString *areaCode = [decimalString substringWithRange:NSMakeRange(index, 3)];
        [formattedString appendFormat:@"(%@) ",areaCode];
        index += 3;
    }

    if (length - index > 3) {
        NSString *prefix = [decimalString substringWithRange:NSMakeRange(index, 3)];
        [formattedString appendFormat:@"%@-",prefix];
        index += 3;
    }

    NSString *remainder = [decimalString substringFromIndex:index];
    [formattedString appendString:remainder];

    textField.text = formattedString;

    return NO;
}
43
bryguy1300

Le code ci-dessous est ce que j'utilise généralement. Le format est différent mais vous obtenez l'image. Cela gérera les entrées telles que '123df # $ @ $ gdfg45- + 678dfg901' et les sorties '1 (234) 567-8901'

#import "NSString+phoneNumber.h"

@implementation NSString (phoneNumber)

-(NSString*) phoneNumber{
    static NSCharacterSet* set = nil;
    if (set == nil){
        set = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
    }
    NSString* phoneString = [[self componentsSeparatedByCharactersInSet:set] componentsJoinedByString:@""];
    switch (phoneString.length) {
        case 7: return [NSString stringWithFormat:@"%@-%@", [phoneString substringToIndex:3], [phoneString substringFromIndex:3]];
        case 10: return [NSString stringWithFormat:@"(%@) %@-%@", [phoneString substringToIndex:3], [phoneString substringWithRange:NSMakeRange(3, 3)],[phoneString substringFromIndex:6]];
        case 11: return [NSString stringWithFormat:@"%@ (%@) %@-%@", [phoneString substringToIndex:1], [phoneString substringWithRange:NSMakeRange(1, 3)], [phoneString substringWithRange:NSMakeRange(4, 3)], [phoneString substringFromIndex:7]];
        case 12: return [NSString stringWithFormat:@"+%@ (%@) %@-%@", [phoneString substringToIndex:2], [phoneString substringWithRange:NSMakeRange(2, 3)], [phoneString substringWithRange:NSMakeRange(5, 3)], [phoneString substringFromIndex:8]];
        default: return nil;
    }
}

@end
15
datinc

Nous avons écrit une sous-classe NSFormatter personnalisée pour les numéros de téléphone ici: https://github.com/edgecase/PhoneNumberFormatter

Vous pouvez l'utiliser comme n'importe quelle autre sous-classe NSFormatter.

10
Michael Enriquez

Merci beaucoup pour la première réponse mais je pense que cette méthode -(int)getLength:(NSString*)mobileNumber est inutile. Vous pouvez essayer quelque chose comme ci-dessous:

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

int length = [[self formatNumber:[textField text]] length];

if (length == 10) {
    if(range.length == 0) {
        return NO;
    }
}

if (length == 3) {
    NSString *num = [self formatNumber:[textField text]];
    textField.text = [NSString stringWithFormat:@"(%@) ",num];
    if (range.length > 0) {
        [textField setText:[NSString stringWithFormat:@"%@",[num substringToIndex:3]]];
    }
}
else if (length == 6) {
    NSString *num = [self formatNumber:[textField text]];
    [textField setText:[NSString stringWithFormat:@"(%@) %@-",[num  substringToIndex:3],[num substringFromIndex:3]]];
    if (range.length > 0) {
        [textField setText:[NSString stringWithFormat:@"(%@) %@",[num substringToIndex:3],[num substringFromIndex:3]]];
    }
}

return YES;
}

- (NSString*)formatNumber:(NSString*)mobileNumber {
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];

int length = [mobileNumber length];

if (length > 10) {
    mobileNumber = [mobileNumber substringFromIndex: length-10];
}

return mobileNumber;
}
6
Yangel

Pour ceux d'entre vous qui ont besoin d'un formatage international des numéros: https://code.google.com/p/libphonenumber/

Livré avec C++, Java et implémentations JavaScript. Il devrait être facile d'envelopper les implémentations C++ dans un fichier .mm et d'écrire un petit wrapper Objective-C autour de lui.

4
ekscrypto

Vous pourriez peut-être utiliser cette méthode simple:

+ (NSString*) formatPhoneNumber:(NSString *)phoneNumber codeLength:(int) code segmentLength:(int) segment
{
    NSString* result = @"";

    int length = [phoneNumber length];

    NSString* firstSegment = @"";
    NSString* restSegment = @"";

    for (int i=0; i<length; i++) {

        char c = [phoneNumber characterAtIndex:i];

        if(i < code)
            firstSegment = [firstSegment stringByAppendingFormat:@"%c", c];
        else
        {
            restSegment = [restSegment stringByAppendingFormat:@"%c", c];

            int threshold = (i - code) + 1;

            if((threshold % segment == 0) && (threshold > 0) && !(threshold > length))
                restSegment = [restSegment stringByAppendingFormat:@"%c", '-'];
        }

    }

    result = [result stringByAppendingFormat:@"%@-%@", firstSegment, restSegment];

    return result;
}

En supposant que la méthode ci-dessus est dans la classe Contact, utilisez simplement la méthode comme ceci:

NSString* phoneNumber = @"085755023455";

NSString* formattedNumber = [Contact formatPhoneNumber:phoneNumber codeLength:3 segmentLength:4];

Ce serait quelque chose comme:

085-7550-2345-5
2
yunhasnawa

Une option valide est https://github.com/iziz/libPhoneNumber-iOS Toutes les autres réponses ne couvrent qu'une petite partie des possibilités et des combinaisons, cette bibliothèque analyse et valide réellement CHAQUE numéro de téléphone, et identifier:

  • Nationalité
  • type de numéro de téléphone
  • Transporteur national
2
Kappe

pertinent pour les numéros de téléphone américains:

Ajout à la publication de @ wan, j'ai ajouté une déclaration conditionnelle si l'utilisateur commence par le code du pays (1). De cette façon, le format sera: 1 (XXX) XXX-XXXX au lieu de (1XX) XXX-XXXX.

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    textField = self.phoneNumberTextField;

    NSInteger length = [self getLength:textField.text];
    //NSLog(@"Length  =  %d ",length);

    if ([textField.text hasPrefix:@"1"]) {
        if(length == 11)
        {
            if(range.length == 0)
                return NO;
        }
        if(length == 4)
        {
            NSString *num = [self formatNumber:textField.text];
            textField.text = [NSString stringWithFormat:@"%@ (%@) ",[num substringToIndex:1],[num substringFromIndex:1]];
            if(range.length > 0)
                textField.text = [NSString stringWithFormat:@"%@",[num substringToIndex:4]];
        }
        else if(length == 7)
        {
            NSString *num = [self formatNumber:textField.text];
            NSRange numRange = NSMakeRange(1, 3);
            textField.text = [NSString stringWithFormat:@"%@ (%@) %@-",[num substringToIndex:1] ,[num substringWithRange:numRange],[num substringFromIndex:4]];
            if(range.length > 0)
                textField.text = [NSString stringWithFormat:@"(%@) %@",[num substringToIndex:3],[num substringFromIndex:3]];
        }

    } else {
        if(length == 10)
        {
            if(range.length == 0)
                return NO;
        }

        if(length == 3)
        {
            NSString *num = [self formatNumber:textField.text];
            textField.text = [NSString stringWithFormat:@"(%@) ",num];
            if(range.length > 0)
                textField.text = [NSString stringWithFormat:@"%@",[num substringToIndex:3]];
        }
        else if(length == 6)
        {
            NSString *num = [self formatNumber:textField.text];

            textField.text = [NSString stringWithFormat:@"(%@) %@-",[num  substringToIndex:3],[num substringFromIndex:3]];
            if(range.length > 0)
                textField.text = [NSString stringWithFormat:@"(%@) %@",[num substringToIndex:3],[num substringFromIndex:3]];
        }
    }
    return YES;
}

-(NSString*)formatNumber:(NSString*)mobileNumber
{
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];

    NSLog(@"%@", mobileNumber);

    NSInteger length = [mobileNumber length];
    if(length > 10)
    {
        mobileNumber = [mobileNumber substringFromIndex: length-10];
        NSLog(@"%@", mobileNumber);

    }


    return mobileNumber;
}
-(NSInteger)getLength:(NSString*)mobileNumber
{

    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];

    NSInteger length = [mobileNumber length];

    return length;        
}
2
rizk.taker

Swift

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {        
            let length = self.getTextLength(textField.text)



            if length == 10{
                if range.length == 0{
                    return false
                }
            }

            if length == 3{

                var num : String = self.formatNumber(textField.text)

                textField.text = num + "-"
                if(range.length > 0){
                    textField.text = (num as NSString).substringToIndex(3)
                }
            }
            else if length == 6{

                var num : String = self.formatNumber(textField.text)

                let prefix  = (num as NSString).substringToIndex(3)
                let postfix = (num as NSString).substringFromIndex(3)

                textField.text = prefix + "-" + postfix + "-"

                if range.length > 0{
                    textField.text = prefix + postfix
                }
            }

            return true
    }




func getTextLength(mobileNo: String) -> NSInteger{

        var str : NSString = mobileNo as NSString
        str = str.stringByReplacingOccurrencesOfString("(", withString: "")
        str = str.stringByReplacingOccurrencesOfString(")", withString: "")
        str = str.stringByReplacingOccurrencesOfString(" ", withString: "")
        str = str.stringByReplacingOccurrencesOfString("-", withString: "")
        str = str.stringByReplacingOccurrencesOfString("+", withString: "")

        return str.length
    }

    func formatNumber(mobileNo: String) -> String{
        var str : NSString = mobileNo as NSString
        str = str.stringByReplacingOccurrencesOfString("(", withString: "")
        str = str.stringByReplacingOccurrencesOfString(")", withString: "")
        str = str.stringByReplacingOccurrencesOfString(" ", withString: "")
        str = str.stringByReplacingOccurrencesOfString("-", withString: "")
        str = str.stringByReplacingOccurrencesOfString("+", withString: "")

        if str.length > 10{
            str = str.substringFromIndex(str.length - 10)
        }

        return str as String
    }
1
Dharmbir Singh

Numéro de téléphone au format Swift

Améliore la réponse de l'adversaire @ datinc, une entrée telle que 1123df#$@$gdfg45-+678dfg901 Sera sortie en tant que +11(234)567-8901

func formattedPhone(phone: String) -> String?  {
    let notPhoneNumbers = NSCharacterSet.decimalDigitCharacterSet().invertedSet
    let str = phone.componentsSeparatedByCharactersInSet(notPhoneNumbers).joinWithSeparator("")

    let startIdx = str.startIndex
    let endIdx = str.endIndex

    let count = str.characters.count
    if count == 7 {
        return "\(str[startIdx..<startIdx.advancedBy(3)])-\(str[startIdx.advancedBy(3)..<endIdx])"
    }else if count == 10{
        return "(\(str[startIdx..<startIdx.advancedBy(3)]))\(str[startIdx.advancedBy(3)..<startIdx.advancedBy(6)])-\(str[startIdx.advancedBy(6)..<endIdx])"
    }
    else if count > 10{
        let extra = str.characters.count - 10
        return "+\(str[startIdx..<startIdx.advancedBy(extra)])(\(str[endIdx.advancedBy(-10)..<endIdx.advancedBy(-7)]))\(str[endIdx.advancedBy(-7)..<endIdx.advancedBy(-4)])-\(str[endIdx.advancedBy(-4)..<endIdx])"
    }
    return nil
}
1
Intentss

Vous pouvez utiliser la bibliothèque AKNumericFormatter pour cela. Il a un formateur et une catégorie UITextField pratique, il est disponible en cocoapod.

1

Cela vous aidera

Format (xxx) xxx-xxxx Pour Swift 3.

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let length = Int(getLength(mobileNumber: textField.text!))

        if length == 15 {
            if range.length == 0 {
                return false
            }
        }

        if length == 3 {

            let num = self.formatNumber(mobileNumber: textField.text!)

            textField.text = NSString(format:"(%@)",num) as String

            if range.length > 0{
                let index: String.Index = num.index(num.startIndex, offsetBy: 3)
                textField.text = NSString(format:"%@",num.substring(to: index)) as String
            }

        }else if length == 6 {
            let num = self.formatNumber(mobileNumber: textField.text!)
            let index: String.Index = num.index(num.startIndex, offsetBy: 3)

            textField.text = NSString(format:"(%@) %@-",num.substring(to: index), num.substring(from: index)) as String
            if range.length > 0{
                textField.text = NSString(format:"(%@) %@",num.substring(to: index), num.substring(from: index)) as String
            }
        }

        return true
    }

    func formatNumber(mobileNumber: String) -> String {
        var number = mobileNumber
        number = number.replacingOccurrences(of: "(", with: "")
        number = number.replacingOccurrences(of: ")", with: "")
        number = number.replacingOccurrences(of: " ", with: "")
        number = number.replacingOccurrences(of: "-", with: "")
        number = number.replacingOccurrences(of: "+", with: "")

        let length = Int(number.characters.count)

        if length > 15 {
            let index = number.index(number.startIndex, offsetBy: 15)

           number = number.substring(to: index)
        }

        return number
    }

    func getLength(mobileNumber: String) -> Int {

        var number = mobileNumber
        number = number.replacingOccurrences(of: "(", with: "")
        number = number.replacingOccurrences(of: ")", with: "")
        number = number.replacingOccurrences(of: " ", with: "")
        number = number.replacingOccurrences(of: "-", with: "")
        number = number.replacingOccurrences(of: "+", with: "")

        let length = Int(number.characters.count)
        return length

    }
1
pradip sutariya

Version C # Xamarin.iOS de réponse la plus complète sur la façon de faire le formatage du téléphone dans iOS est ci-dessous

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        PhoneNumberTextField.ShouldChangeCharacters = ChangeCharacters;
    }

    private bool ChangeCharacters(UITextField textField, NSRange range, string replacementString)
    {
        var text = textField.Text;
        var newString = text.Substring(0, range.Location) + replacementString + text.Substring(range.Location + range.Length);
        var decimalString = Regex.Replace(newString, @"[^\d]", string.Empty);
        var length = decimalString.Length;
        var hasLeadingOne = length > 0 && decimalString[0] == '1';
        if ((length == 0) || (length > 10 && !hasLeadingOne) || (length > 11))
        {
            textField.Text = decimalString;
            return false;
        }
        var index = 0;
        var formattedString = "";
        if (hasLeadingOne)
        {
            formattedString += "1";
            index += 1;
        }
        if (length - index > 3)
        {
            var areaCode = decimalString.Substring(index, 3);
            formattedString += "(" + areaCode + ")";
            index += 3;
        }
        if (length - index > 3)
        {
            var prefix = decimalString.Substring(index, 3);
            formattedString += " " + prefix + "-";
            index += 3;
        }
        var remainder = decimalString.Substring(index);
        formattedString += remainder;
        textField.Text = formattedString;
        return false;
    }
1
Alex Sorokoletov

Tout d'abord, ajoutez UITextFieldDelegate à votre .h et déléguez votre UITextField dans le fichier nib.

Ensuite, ajoutez ce code à votre .m fichier :

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
        {
            NSString *filter = @"(###)-(###)-####";

            if(!filter) return YES;

            NSString *changedString = [textField.text stringByReplacingCharactersInRange:range withString:string];

            if(range.length == 1 && 
               string.length < range.length &&
               [[textField.text substringWithRange:range] rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"0123456789"]].location == NSNotFound)
            {

                NSInteger location = changedString.length-1;
                if(location > 0)
                {
                    for(; location > 0; location--)
                    {
                        if(isdigit([changedString characterAtIndex:location]))
                        {
                            break;
                        }
                    }
                    changedString = [changedString substringToIndex:location];
                }
            }

            textField.text = [self filteredPhoneStringFromStringWithFilter:changedString :filter];

            return NO;

        }

    -(NSString*) filteredPhoneStringFromStringWithFilter:(NSString*)number : (NSString*)filter{
        NSUInteger onOriginal = 0, onFilter = 0, onOutput = 0;
        char outputString[([filter length])];
        BOOL done = NO;

    while(onFilter < [filter length] && !done)
    {
        char filterChar = [filter characterAtIndex:onFilter];
        char originalChar = onOriginal >= number.length ? '\0' : [number characterAtIndex:onOriginal];
        switch (filterChar) {
            case '#':
                if(originalChar=='\0')
                {
                    // We have no more input numbers for the filter.  We're done.
                    done = YES;
                    break;
                }
                if(isdigit(originalChar))
                {
                    outputString[onOutput] = originalChar;
                    onOriginal++;
                    onFilter++;
                    onOutput++;
                }
                else
                {
                    onOriginal++;
                }
                break;
            default:
                // Any other character will automatically be inserted for the user as they type (spaces, - etc..) or deleted as they delete if there are more numbers to come.
                outputString[onOutput] = filterChar;
                onOutput++;
                onFilter++;
                if(originalChar == filterChar)
                    onOriginal++;
                break;
        }
    }
    outputString[onOutput] = '\0'; // Cap the output string
    return [NSString stringWithUTF8String:outputString];
}
0
Rudi

Dans Swift

func formattedPhone(phone: String) -> String?  {
    let notPhoneNumbers = CharacterSet.decimalDigits.inverted
    let str = phone.components(separatedBy: notPhoneNumbers).joined(separator: "")

    let startIdx = str.startIndex
    let endIdx = str.endIndex

    let count = str.characters.count
    if count == 7 {
        return "\(str[startIdx..<startIdx.advance(3, for: str)])-\(str[startIdx.advance(3, for: str)..<endIdx])"
    }else if count == 10{
        return "+1 (\(str[startIdx..<startIdx.advance(3, for: str)])) \(str[startIdx.advance(3, for: str)..<startIdx.advance(6, for: str)])-\(str[startIdx.advance(6, for: str)..<endIdx])"
    }
    else if count > 10{
        let extra = str.characters.count - 10
        return "+\(str[startIdx..<startIdx.advance(extra, for: str)]) (\(str[endIdx.advance(-10, for: str)..<endIdx.advance(-7, for: str)])) \(str[endIdx.advance(-7, for: str)..<endIdx.advance(-4, for: str)])-\(str[endIdx.advance(-4, for: str)..<endIdx])"
    }
    return nil
}

Swift 3 string.index.advancedBy (3) alternatif:

extension String.Index{
func advance(_ offset:Int, `for` string:String)->String.Index{
    return string.index(self, offsetBy: offset)
}
}
0
SaRaVaNaN DM

REFormattedNumberField est probablement le meilleur. Fournissez simplement le format que vous souhaitez.

0
ReDetection
  1. Supprime tous les caractères non numériques
  2. S'il reste 7 chiffres, 123-4567
  3. pour 10 chiffres, (123) 456-7890
  4. Sinon, des groupes de trois. Pour ajuster la taille des groupes, modifiez la valeur affectée à la sous-taille

    -(NSString*)formatPhone:(NSString*)phone {
    
         NSString *formattedNumber = [[phone componentsSeparatedByCharactersInSet:
                              [[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
                             componentsJoinedByString:@""];
    
    int substrSize = 3;
    NSMutableArray *t = [[NSMutableArray alloc] initWithCapacity:formattedNumber.length / substrSize + 1];
    switch (formattedNumber.length) {
        case 7:
            formattedNumber = [NSString stringWithFormat:@"%@-%@",
                           [formattedNumber substringToIndex:3],
                           [formattedNumber substringFromIndex:3]];
        break;
    
        case 10:
            formattedNumber = [NSString stringWithFormat:@"(%@) %@-%@",
                           [formattedNumber substringToIndex:3],
                           [formattedNumber substringWithRange:NSMakeRange(3, 3)],
                           [formattedNumber substringFromIndex:6]];
        break;
    
        default:
            for (int i = 0; i < formattedNumber.length / substrSize; i++) {
            [t addObject:[formattedNumber substringWithRange:NSMakeRange(i * substrSize, substrSize)]];
            }
            if (formattedNumber.length % substrSize) {
                [t addObject:[formattedNumber substringFromIndex:(substrSize * t.count)]];
        }
            formattedNumber = [t componentsJoinedByString:@" "];
        break;
     }
     return formattedNumber;
    }
    
0
56phil
NSString *str=@"[+]+91[0-9]{10}";
NSPredicate *no=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",str];
if([no evaluateWithObject:txtMobileno.text]==NO
{ 
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Warning" message:@"Please Enter correct contact no." delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];

    [alert show];
    [alert release];    
}
0
upendra

Cette méthode formatera donc (xxx) xxx - xxxx ....
c'est une modification de la première réponse actuelle et gère les backspaces

- (IBAction)autoFormat:(UITextField *)sender {

NSString *mobileNumber = [NSString stringWithFormat:@"%@",sender.text];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];

int length = [mobileNumber length];
if(length > 0 && [sender.text length] > self.oldLength){
    if(length >= 7 && length <= 10){
        sender.text = [NSString stringWithFormat:@"(%@) %@ - %@",[mobileNumber substringToIndex:3], [mobileNumber substringWithRange:NSMakeRange(3,3)],[mobileNumber substringWithRange:NSMakeRange(6,[mobileNumber length]-6)]];
    } else if(length >= 4 && length <= 6) {
        sender.text = [NSString stringWithFormat:@"(%@) %@",[mobileNumber substringToIndex:3], [mobileNumber substringWithRange:NSMakeRange(3,[mobileNumber length]-3)]];
    }
    if(length >= 11 && length % 4 == 3){
        NSString *lastChar = [sender.text substringFromIndex:[sender.text length] - 1];
        sender.text = [NSString stringWithFormat:@"%@ %@",[sender.text substringToIndex:[sender.text length] - 1],lastChar];
    }
    self.oldLength = [sender.text length];
} else if([sender.text length] < self.oldLength) {
    NSLog(@"deleted - ");
    self.oldLength = 0;

    sender.text = @"";
    for (int i = 0; i < [mobileNumber length]; i = i + 1) {
        sender.text = [NSString stringWithFormat:@"%@%@",sender.text,[mobileNumber substringWithRange:NSMakeRange(i, 1)]];
        [self autoFormat:sender];
    }
}}

j'espère que ça aide

0
Bill

Aussi pour le format + x (xxx) xxx-xx-xx vous pouvez utiliser quelque chose comme cette solution simple:

+ (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSArray *components = [newString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
NSString *decimalString = [components componentsJoinedByString:@""];

if (decimalString.length > 11) {
    return NO;
}

NSMutableString *formattedString = [NSMutableString stringWithString:decimalString];

[formattedString insertString:@"+" atIndex:0];

if (formattedString.length > 2)
    [formattedString insertString:@" (" atIndex:2];

if (formattedString.length > 7)
    [formattedString insertString:@") " atIndex:7];

if (formattedString.length > 12)
    [formattedString insertString:@"-" atIndex:12];

if (formattedString.length > 15)
    [formattedString insertString:@"-" atIndex:15];


textField.text = formattedString;
return NO;}      
0
Aleksei Sakharov
+(NSString *) phoneNumberFormatterTextField:(NSString *)number forRange:(NSRange)range
{
    int length = (int)[[self getPhoneNumber:number] length];
    if(length == 3)
    {
        NSString *num = [MPosBaseScreenController getPhoneNumber:number];
        number = [num stringByReplacingOccurrencesOfString:@"(\\d{3})"
                                              withString:@"($1) "
                                                 options:NSRegularExpressionSearch
                                                   range:NSMakeRange(0, num.length)];

    }
    else if(length == 6 || length > 6 )
    {
        NSString *num = [MPosBaseScreenController getPhoneNumber:number];
        number = [num stringByReplacingOccurrencesOfString:@"(\\d{3})(\\d{3})"
                                              withString:@"($1) $2 - "
                                                 options:NSRegularExpressionSearch
                                                   range:NSMakeRange(0, num.length)];
    }
       return number;
}
0
Ashim Dahal

Voici une catégorie simple qui imitera le format de l'entrée

@interface NSString (formatDecimalsAs)
- (NSString *)formatDecimalsAs:(NSString *)formattedDecimals;
@end

@implementation NSString (formatDecimalsAs)
- (NSString *)formatDecimalsAs:(NSString *)formattedDecimals
{
    // insert non-digit characters from source string
    NSMutableString *formattedNumber = [self mutableCopy];
    for (int i = 0; i < formattedDecimals.length; i++)
    {
        if (i > formattedNumber.length)
        {
            break;
        }
        unichar character = [formattedDecimals characterAtIndex:i];
        if ([[NSCharacterSet decimalDigitCharacterSet].invertedSet characterIsMember:character])
        {
            [formattedNumber insertString:[NSString stringWithFormat:@"%c", character] atIndex:(NSUInteger) i];
        }
    }
    return formattedNumber;
}
@end

exemple d'utilisation

[@"87654321" formatDecimalsAs:@"1111 1111"] // returns @"8765 4321"
0
Henrik Hartz