在文本字段中自动添加连字符但无法编辑文本字段(电话号码屏蔽)

时间:2015-06-24 04:38:41

标签: ios objective-c uitextfield phone-number masking

使用以下代码,我可以自动添加连字符,但无法在文本字段中正确编辑。

例如,当我单击在文本字段中已输入的数字之间插入任何数字时,这将在文本字段的末尾插入数字,并且不会正确删除该数字。任何人都可以帮助我吗?

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  {    
     if(textField == txtUserName)
     {
         if (range.location == 12)
         {
             return NO;
         }
         if (range.length == 0 && ![[NSCharacterSet decimalDigitCharacterSet]   characterIsMember:[string characterAtIndex:0]])
         {
             return NO;
         }       
        if (range.length == 0 && (range.location == 3 || range.location == 7))
         {
             txtUserName.text = [NSString stringWithFormat:@"%@-  %@",txtUserName.text,string];
             return NO;
         }
         if (range.length == 1 &&(range.location==4 ||range.location ==7))      
         {
             range.location--;
             range.length = 2;
              txtUserName.text = [txtUserName.text  stringByReplacingCharactersInRange:range withString:@""];
             NSLog(@"Nisha..%@",txtUserName.text);
             return NO;
          }
    }   
    return YES;
}

3 个答案:

答案 0 :(得分:2)

当我们在UITextField中复制粘贴文本时,此代码也可以使用。

  

警告此代码不适用于 键盘的删除按钮 。无论如何,您只需要 键盘的退格按钮 ,因为 移动键盘的删除按钮就像键盘的退格按钮一样

注意 - tag使用UITextField,而不是直接将textField与您的商店进行比较

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //(textField == txtUserName)
    // don't compare with ID, use tag
    if(textField.tag == 0 ) 
    {
        NSString *text = textField.text;
        NSUInteger textLength = text.length;
        NSString *trimText = [text stringByReplacingOccurrencesOfString:@"-" withString:@""];
        NSUInteger trimTextLength = trimText.length;

        if (range.length == 0 && [string rangeOfCharacterFromSet: [[NSCharacterSet decimalDigitCharacterSet] invertedSet]].location != NSNotFound)
        {
            return NO;
        }
        if( string.length > 0 ) {
            NSString *replacedText = text;
            if(trimTextLength < 10) {
                NSInteger remainingNumbers = (10-trimTextLength);
                if(string.length > remainingNumbers) {
                    string = [string substringToIndex:remainingNumbers];
                }
                replacedText = [replacedText stringByReplacingCharactersInRange:range withString:string];
            }
            NSString *trimReplacedText = [replacedText stringByReplacingOccurrencesOfString:@"-" withString:@""];
            if( trimReplacedText.length > 3 ) {
                trimReplacedText = [trimReplacedText stringByReplacingCharactersInRange:NSMakeRange(3, 0) withString:@"-"];
            }
            if( trimReplacedText.length > 7 ) {
                trimReplacedText = [trimReplacedText stringByReplacingCharactersInRange:NSMakeRange(7, 0) withString:@"-"];
            }
            textField.text = trimReplacedText;
            return NO;
        }
        bool flag = false;
        if (range.length == 1 &&(range.location == 4 || (range.location == 7 && (textLength-trimTextLength)== 1 && [text rangeOfString:@"-"].location == 7) || (range.location == 8 && (textLength-trimTextLength)== 2) ))
        {
            range.location--;
            range.length = 2;
            flag = true;
        }
        else if (range.length >= 1)
        {
            flag = true;
        }

        if(flag) {
            NSString *replacedText = [textField.text stringByReplacingCharactersInRange:range withString:@""];

            NSString *trimReplacedText = [replacedText stringByReplacingOccurrencesOfString:@"-" withString:@""];
            if( trimReplacedText.length > 3 ) {
                trimReplacedText = [trimReplacedText stringByReplacingCharactersInRange:NSMakeRange(3, 0) withString:@"-"];
            }
            if( trimReplacedText.length > 7 ) {
                trimReplacedText = [trimReplacedText stringByReplacingCharactersInRange:NSMakeRange(7, 0) withString:@"-"];
            }
            textField.text = trimReplacedText;
            return NO;
        }
    }
    return YES;
}

答案 1 :(得分:0)

  1. 观察UITextFieldTextDidChangeNotification。受影响的文本字段存储在通知的object参数中。

  2. 检查notification.object.text的文本格式(如果需要,请插入连字符

答案 2 :(得分:0)

即使这个问题太旧了,但如果您需要帮助,可以使用VMaskTextField库

https://github.com/viniciusmo/VMaskTextField