在APP中,注册输入手机号、输入银行卡进行绑卡的操作是很常见的。下面介绍我的对输入框输入数字进行4位加一个空格的方法
思路
- 有3种情况需要处理【1. 键盘输入时 2.粘贴 3.KVO监听到赋值 】
- 因为多个页面会使用到这个功能,自己需要抽取方法;
- 设置textfield的代理,在代理方法里面让textField调用抽取方法进行格式化
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
- KVO监听textField的text变化,在手动设置textField.text时,手动调用上述代理方法实现格式化
- 通常在用户输入text或者设置text后需要执行自己的业务逻辑,此时通过block回调的方式解决
具体实现
- 新建UITextField分类,抽取方法放在此分类下
/** 将输入框内用户输入的文字进行4位1空格格式化
*/
- (BOOL)bankCardFormatWithInputRange:(NSRange)range replacementString:(NSString *)string successInput:(void (^)())successInputBlock{
// 每次输入了4位之后,如果此时是正在输入,则自动增加空格,如果正在删除,则自动删除空格!!!
NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"];// "\b"=退格
// 先判断准备添加\替换的字符是不是合法
string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {// 找到了非法字符
return NO;
}
NSString *text = self.text;
text = [text stringByReplacingCharactersInRange:range withString:string];
text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
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 > 24) {
newString = [newString substringWithRange:NSMakeRange(0, 24)];
}
self.text = newString;
successInputBlock();
return NO;
}
/** 将输入框内用户输入的文字手机号码格式化 xxx xxxx xxxx
*/
- (BOOL)phoneNumberFormatWithInputRange:(NSRange)range replacementString:(NSString *)string successInput:(void (^)())successInputBlock{
/**1.当到第四位或第九位时,如果此时是正在输入,则自动增加空格,如果正在删除,则自动删除空格!!!
*2.当到第13位时,截取前面的13位字符串,
*3.若想要获取输入的手机,需要先删除空格;
*/
NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"];// "\b"=退格
// 先判断准备添加\替换的字符是不是合法
string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {
return NO;
}
NSString *text = self.text;
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 > 13) {
newString = [newString substringWithRange:NSMakeRange(0, 13)];
}
self.text = newString;
successInputBlock();
return NO;
}
- 具体类中使用
// 添加kvo监听
[self.phoneField addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
// 移除kvo监听,不移除在iOS9以下会闪退
- (void)dealloc{
[self.phoneField removeObserver:self forKeyPath:@"text"];
}
// 监听到了textField改变时
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
if (object == self.phoneField && [keyPath isEqualToString:@"text"]) {
NSString *newNumber = change[NSKeyValueChangeNewKey];
NSString *oldNumber = change[NSKeyValueChangeOldKey];
if (![newNumber isEqualToString:oldNumber]) {
[self textField:object shouldChangeCharactersInRange:NSMakeRange(0, [(UITextField *)object text].length) replacementString:newNumber];
}
}else{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
// 以下是代理方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if (textField == self.phoneField) {
// 1. 键盘输入时 2.粘贴 3.KVO赋值 4.清除按钮点击
return [textField phoneNumberFormatWithInputRange:range replacementString:string successInput:^{
[self refreshSureBtnState];
}];
}else if(textField == self.bankCardNoField){
return [textField bankCardFormatWithInputRange:range replacementString:string successInput:^{
[self refreshSureBtnState];
}];
}
return YES;
}
- 以上完结
网友评论