如何让textField输入框 限制输入字数的同时,还只让输入数字和字母
推荐于2016-05-16
展开全部
两个代理方法 一个是点击return 缩回键盘
还有一个是对textField的输入进行监听
前提是需要对textField进行代理监听
//UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
{
//用户结束输入
[textField resignFirstResponder];
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange NSRange)range replacementString NSString*)string // return NO to not change text
{
//判断是否超过 ACCOUNT_MAX_CHARS 个字符,注意要判断当string.leng>0
//的情况才行,如果是删除的时候,string.length==0
int length = textField.text.length;
if (length >= ACCOUNT_MAX_CHARS && string.length >0)
{
return NO;
}
NSCharacterSet *cs;
cs = [[NSCharacterSet characterSetWithCharactersInString:kAlphaNum] invertedSet];
NSString *filtered =
[[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
BOOL basic = [string isEqualToString:filtered];
return basic;
}
还有一个是对textField的输入进行监听
前提是需要对textField进行代理监听
//UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
{
//用户结束输入
[textField resignFirstResponder];
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange NSRange)range replacementString NSString*)string // return NO to not change text
{
//判断是否超过 ACCOUNT_MAX_CHARS 个字符,注意要判断当string.leng>0
//的情况才行,如果是删除的时候,string.length==0
int length = textField.text.length;
if (length >= ACCOUNT_MAX_CHARS && string.length >0)
{
return NO;
}
NSCharacterSet *cs;
cs = [[NSCharacterSet characterSetWithCharactersInString:kAlphaNum] invertedSet];
NSString *filtered =
[[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
BOOL basic = [string isEqualToString:filtered];
return basic;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询