fjcaetano / NSStringMask

NSStringMask allows you to apply masks or formats to NSStrings using NSRegularExpression to input your format.
http://fjcaetano.github.io/NSStringMask/
MIT License
241 stars 36 forks source link

Mask para Telefone com 9 digitos #4

Closed gubertu closed 9 years ago

gubertu commented 10 years ago

Olá @fjcaetano,

Estou tentando fazer a mask para telefone com 9 digitos, porem não está funcionando!!

Estou fazendo da seguinte forma :

- (void)viewDidLoad {

    _maskTel = [NSStringMask maskWithPattern:@"\\((\\d{2})\\) (\\d{4})-(\\d{4})" placeholder:@"_"];
    _maskTelSP = [NSStringMask maskWithPattern:@"\\((\\d{2})\\) (\\d{5})-(\\d{4})" placeholder:@"_"];
    textFieldTelefone.delegate = self;
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if(textField == textFieldTelefone) {
    NSString* newText = [textField.text stringByReplacingCharactersInRange:range withString:string];
    newText = [newText stringByReplacingOccurrencesOfString:@"_" withString:@""];
    newText = [newText stringByReplacingOccurrencesOfString:@"(" withString:@""];
    newText = [newText stringByReplacingOccurrencesOfString:@")" withString:@""];
    newText = [newText stringByReplacingOccurrencesOfString:@" " withString:@""];
    newText = [newText stringByReplacingOccurrencesOfString:@"-" withString:@""];

    textFieldTelefone.mask = (newText.length > 10 ? self.maskTelSP : self.maskTel);
    return YES;
}
return YES;

}

No caso a mascara fica desse jeito : (11) 3233-9442

Estou fazendo o replace para que fique somente o numero : 1132339442

Porém quando digito 9 digitos não funciona!!

fjcaetano commented 10 years ago

@gubertu o método validCharactersForString: retorna apenas os caracteres válidos para uma máscara, experimente verificar o tamanho do número de telefone usando este método.

gubertu commented 10 years ago

@fjcaetano olha como fiz:

  -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if(textField == textFieldTelefone) {
       NSString *newText = [textFieldTelefone.mask validCharactersForString:textField.text];
       textFieldTelefone.mask = (newText.length > 9 ? self.maskTelSP : self.maskTel);
       return YES;
    }
return YES;

}

As mask estão declarada assim:

_maskTel = [NSStringMask maskWithPattern:@"\\((\\d{2})\\) (\\d{4})-(\\d{4})" placeholder:@"_"];
_maskTelSP = [NSStringMask maskWithPattern:@"\\((\\d{2})\\) (\\d{5})-(\\d{4})" placeholder:@"_"];

Assim ta funcionando ate chegar no último digito, que ta apareceno esse erro no console:

Error Domain=NSCocoaErrorDomain Code=2048 "The operation couldn’t be completed. (Cocoa error  2048.)" UserInfo=0x15fb1de0 {NSInvalidValue=\d{1,-4}}
2014-05-09 12:48:20.219 Net[5096:60b] Error Domain=NSCocoaErrorDomain Code=2048 "The operation couldn’t be completed. (Cocoa error 2048.)" UserInfo=0x15fb1de0 {NSInvalidValue=\d{1,-4}}
2014-05-09 12:48:20.226 Net[5096:60b] Error Domain=NSCocoaErrorDomain Code=2048 "The operation couldn’t be completed. (Cocoa error 2048.)" UserInfo=0x15fa7000 {NSInvalidValue=\d{1,-4}}
2014-05-09 12:48:20.229 Net[5096:60b] Error Domain=NSCocoaErrorDomain Code=2048 "The operation couldn’t be completed. (Cocoa error 2048.)" UserInfo=0x15fa7000 {NSInvalidValue=\d{1,-4}}
fjcaetano commented 9 years ago

Fechando por inatividade.