1.为uitextfield添加点击事件
2.点击事件内容如下
-(void)reformatAsCardNumber:(UITextField *)textField {
NSString *fieldStr = textField.text;
if (_count < textField.text.length) {
//增加
if (_count == 3 || _count == 8) {
NSMutableString *str = [NSMutableString stringWithFormat:@"%@",textField.text];
[str insertString:@" " atIndex:_count];
textField.text = str;
}
}else if (_count > textField.text.length) {
//减少
if (_count == 5 || _count == 10) {
NSMutableString *str = [NSMutableString stringWithFormat:@"%@",textField.text];
textField.text = [str substringToIndex:_count -2];
}
}
_count = textField.text.length;
if (fieldStr.length > 12) {
fieldStr = [fieldStr substringToIndex:13];
textField.text = fieldStr;
}
}