最近业务要求输入字母的同时必须强制大写,而且不能录入中文,点击提交的时候判断还不行,必须要求输入的同时判断匹配
- (void)textFieldChangeValue:(UITextField *)textfield{
if ([[MBISingletonCacheData retrieveUserSegmentId] isEqualToString:@"91"]) {
if ([[UITextInputMode currentInputMode].primaryLanguage isEqualToString:@"zh-Hans"]) {//这个方法在iOS 7 被废弃,但是可以使用
return;
}else{
textfield.text = [textfield.text uppercaseString];
}
}
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ((textField.tag == 666 || textField.tag == 777)&&[[MBISingletonCacheData retrieveUserSegmentId]isEqualToString:@"91"]) { // 判断输入框是当前需要判断控制的输入框
NSCharacterSet *set = [[NSCharacterSet characterSetWithCharactersInString:ALPHA] invertedSet];
NSString *filterd = [[string componentsSeparatedByCharactersInSet:set] componentsJoinedByString:@""];
return [string isEqualToString:filterd];
}
return false;
}
ALPHA 使用宏定义写好 def ALPHA @"QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm 1234567890"