IOS textField怎样设置只能输入英文字母或者数字,不能输入汉字

兄弟们,请问一下,IOS textField怎样设置只能输入英文字母或者数字,不能输入汉字
最新回答
游戏人间

2024-10-31 02:39:21

//判断是否是数字,不是的话就输入失败
- (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;