// Copyright © 2016年 GQ. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UITextFieldDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
/**
* 这个方法在textField的text被改变时调用,如果return NO,那么此次输入被忽略,YES,此次输入被添加
*
* @param textField 记住这个是改变之前的textFiled
* @param range 指的是要输入字符串的位置,比如用户在字符串之间进行插入,比如
range = {4, 2}
* @param string 本次输入的字符串
*
*/
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
BOOL returnValue = YES;
NSMutableString* newText = [NSMutableStringstringWithCapacity:0];
[newText appendString:textField.text];// 拿到原有text,根据下面判断可能给它添加" "(空格);
NSString * noBlankStr = [textField.textstringByReplacingOccurrencesOfString:@" "withString:@""];
NSInteger textLength = [noBlankStr length];
if (string.length) {
if (textLength < 25) {//这个25是控制实际字符串长度,比如银行卡号长度
if (textLength > 0 && textLength %4 == 0) {
newText = [NSMutableString stringWithString:[newText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
[newTextappendString:@" "];
[newTextappendString:string];
textField.text = newText;
returnValue = NO;//为什么return NO?因为textField.text = newText;text已经被我们替换好了,那么就不需要系统帮我们添加了,如果你ruturnYES的话,你会发现会多出一个字符串
}else {
[newTextappendString:string];
}
}else { // 比25长的话 return NO这样输入就无效了
returnValue =NO;
}
}else { // 如果输入为空,该怎么地怎么地
[newText replaceCharactersInRange:rangewithString:string];
}
return returnValue;
}
@end