IOS textField怎样设置只能输入英文字母或者数字,不能输入汉字
参考资料:
http://segmentfault.com/q/1010000002705645/a-1020000002708433
//设置键盘类型
self.textField.keyboardType = UIKeyboardTypeASCIICapable;
define kAlphaNum @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
//判断是否是数字,不是的话就输入失败
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *cs;
cs = [[NSCharacterSet characterSetWithCharactersInString:kAlphaNum] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串
BOOL canChange = [string isEqualToString:filtered];
return self.textField.text.length>=5?NO: canChange;
}