UITextField实现输入手机号时自动添加空格

背景
为了优化用户体验,我们往往会在让用户输入手机号码时添加空格,比如:151 6558 1234.那么在iOS中如何实现呢?

实现方法
iOS中的输入框给UITextField添加UIControlEventEditingChanged事件 ,该方法实现输入框文字变动时的监听:textFieldDidEditing:

.DEMO

NSInteger i;//定义全局变量

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    i = 0;

    [self.textField addTarget:self action:@selector(textFieldDidEditing:) forControlEvents:UIControlEventEditingChanged];
    }
    -(void)textFieldDidEditing:(UITextField *)textField{
    if (textField == self.textField) {
    if (textField.text.length > i) {
    if (textField.text.length == 4 || textField.text.length == 9 ) {//输入
    NSMutableString * str = [[NSMutableString alloc ] initWithString:textField.text];
    [str insertString:@" " atIndex:(textField.text.length-1)];
    textField.text = str;
    }if (textField.text.length >= 13 ) {//输入完成
    textField.text = [textField.text substringToIndex:13];
    [textField resignFirstResponder];
    }
    i = textField.text.length;

    }else if (textField.text.length < i){//删除
        if (textField.text.length == 4 || textField.text.length == 9) {
            textField.text = [NSString stringWithFormat:@"%@",textField.text];
            textField.text = [textField.text substringToIndex:(textField.text.length-1)];
        }
        i = textField.text.length;
    }
    

    }
    }

ok,上面demo便可实现实现输入手机号时自动添加空格,

//备注:当到第四位或第九位时,如果此时是正在输入,则自动增加空格,如果正在删除,则自动删除空格!!!

//当到第13位时,截取前面的13位字符串,收起键盘

//若想要获取输入的手机,需要先删除空格; NSString *textFieldStr =[self.textField.text stringByReplacingOccurrencesOfString:@" "withString:@""];

方法二:
UITextField格式化电话号码和银行卡

//最近又发现了个新的方法也还不错哦

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

NSString *text = [textField text];

NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"];

string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];

if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {

    return NO;

}



text = [text stringByReplacingCharactersInRange:range withString:string];

text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];

// 如果是电话号码格式化,需要添加这三行代码

NSMutableString *temString = [NSMutableString stringWithString:text];

[temString insertString:@" " atIndex:0];

text = temString;

NSString *newString = @"";

while (text.length > 0) {

    NSString *subString = [text substringToIndex:MIN(text.length, 4)];

    newString = [newString stringByAppendingString:subString];

    if (subString.length == 4) {

        newString = [newString stringByAppendingString:@" "];

    }

    text = [text substringFromIndex:MIN(text.length, 4)];

}



newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];



if (newString.length >= 14) {

    return NO;

}

[textField setText:newString];

return NO;

}

这段代码是极好的:不但一个个输入字符时没有问题,而且可以实现,复制粘贴过来后再实现格式化

相关推荐

微信扫一扫,分享到朋友圈

UITextField实现输入手机号时自动添加空格
返回顶部

显示

忘记密码?

显示

显示

获取验证码

Close