一般app至少都有10几个输入框,多的几十个,这时候我们在一个个写的时候有时候就会觉得很麻烦,为什么呢,因为很多都需要做限制,比如只能输入数字,输入的长度不能超过11(电话号码),长度不能超过6(验证码),长度不能超过20(密码),长度不能超过50(数据库字符限制)等等。你一个个的写很烦很头疼很多垃圾代码怎么办呢?
我忍无可忍自己写了一个稍微方便点的,需求就是尽量我在调用的时候用一句话就可以达到我的需求。
我最开始写了个category直接声明textField发现不行答不到我的需求,不能直接写监听和代理还得到子类去实现。于是我用了一个view来装textField。直接上代码吧:
typedef enum KZWTextFieldType { KZWTextFieldPhone, KZWTextFieldCode, KZWTextFieldPWD, KZWTextFieldDefault } KZWTextFieldType; @interface KZWBaseTextField : UIView - (instancetype)initWithFrame:(CGRect)frame font:(CGFloat)font keyboardType:(UIKeyboardType)keyboardType placeholder:(NSString *)placeholder KZWTextFieldType:(KZWTextFieldType)type; @property (strong, nonatomic) UITextField *textField; @property (copy, nonatomic) NSString *text; - (void)setText:(NSString *) text; @end
这是.h的代码,一个枚举对一些特殊类型的处理,分别是手机,验证码,密码和默认。声明的方法里参数分别是frame,font,keyboardType,placeholder,KZWTextFieldType,属性是textField的text和setText。
#import "KZWBaseTextField.h" @interface KZWBaseTextField()@property (assign, nonatomic) KZWTextFieldType type; @end @implementation KZWBaseTextField - (instancetype)initWithFrame:(CGRect)frame font:(CGFloat)font keyboardType:(UIKeyboardType)keyboardType placeholder:(NSString *)placeholder KZWTextFieldType:(KZWTextFieldType)type{ if (self = [super initWithFrame:frame]) { [self addSubview:self.textField]; self.textField.frame = CGRectMake(0, 0, frame.size.width, frame.size.height); self.textField.font = [UIFont systemFontOfSize:font]; self.textField.keyboardType = keyboardType; self.textField.placeholder = placeholder; self.type = type; } return self; } - (UITextField *)textField { if (!_textField) { _textField = [[UITextField alloc] init]; _textField.clearButtonMode = UITextFieldViewModeWhileEditing; _textField.borderStyle = UITextBorderStyleNone; _textField.backgroundColor = [UIColor whiteColor]; _textField.returnKeyType = UIReturnKeyDone; _textField.textColor = [UIColor colorWithHexString:FontColor333333]; _textField.delegate = self; [_textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; } return _textField; } - (void)textFieldDidChange:(UITextField *)textField { NSString *toBeString = textField.text; toBeString = [self filterCharactor:textField.text withRegex:@"[^a-zA-Z0-9/u4e00-/u9fa5]"]; NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; if([lang isEqualToString:@"zh-Hans"]) { UITextRange *selectedRange = [textField markedTextRange]; UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0]; if(!position) { textField.text = [self lengthLimit:toBeString]; } else{ } } else{ textField.text = [self lengthLimit:toBeString]; } } - (NSString *)lengthLimit:(NSString *)toBeString { switch (self.type) { case KZWTextFieldIdCard: { if(toBeString.length > 18) { return [toBeString substringToIndex:18]; } } break; case KZWTextFieldPhone: { if(toBeString.length > 11) { return [toBeString substringToIndex:11]; } } break; case KZWTextFieldPWD: { if(toBeString.length > 20) { return [toBeString substringToIndex:20]; } } break; case KZWTextFieldCode: { if(toBeString.length > 6) { return [toBeString substringToIndex:6]; } } break; case KZWTextFieldDefault: { if(toBeString.length > 50) { return [toBeString substringToIndex:50]; } } default: break; } return toBeString; } - (NSString *)text { return self.textField.text; } - (void)setText:(NSString *)text { self.textField.text = text; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } - (NSString *)filterCharactor:(NSString *)string withRegex:(NSString *)regexStr{ NSString *filterText = string; NSError *error = NULL; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:&error]; NSString *result = [regex stringByReplacingMatchesInString:filterText options:NSMatchingReportCompletion range:NSMakeRange(0, filterText.length) withTemplate:@""]; return result; } @end
.m的代码初始化,textField的声明,textFieldDidChange长度处理、过滤掉非字母/数字/汉子,代理点done落下键盘。这样基本已经完美的达到我自己的需求了。什么边缘测试,特殊字符测试都统统没问题。
最后看下调用
self.nameTextField = [[KZWBaseTextField alloc] initWithFrame:CGRectMake(113, 30, SCREEN_WIDTH - 113, 52) font:FontSize30 keyboardType:UIKeyboardTypeDefault placeholder:@"请填写真实姓名" KZWTextFieldType:KZWTextFieldDefault]; [footView addSubview:self.nameTextField]; self.ocrModel.name = self.nameTextField.text;
完!