码迷,mamicode.com
首页 > 其他好文 > 详细

要求UItextField指数如中文、数字、英文

时间:2015-04-29 19:51:36      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

在开发中我们有时候会对UITextFiled 进行一些操作,说一下如何只输入中文,或者 只输入 数字 和英文 2中方法


我直接用宏定义定义:

#define kAlphaNum @"abcdefghijklmnopqrstuvwxyz" //这种是对应只输入中文 不允许其他数字或者符号的

#define kEnglishNum @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" //这种是对应只能输入数字和英文的方法


执行UITextField 的delegate 方法 切记 textFiled.delegate = self;


下面直接上代码:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{

UITextField *nameField = (UITextField *)[self viewWithTag:200];

UITextField *cardField = (UITextField *)[self viewWithTag:201];


if (textField == nameField) {

//只允许输入中文

NSCharacterSet *cs;

cs = [[NSCharacterSet characterSetWithCharactersInString:kAlphaNum] invertedSet];

NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];

BOOL basic = [string isEqualToString:filtered];

NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];

if (_delegate && [_delegate respondsToSelector:@selector(passName:)]) {

[_delegate passName:text];

}

return basic;

}


if (textField == cardField) {


//只允许输入英文和数字

NSCharacterSet *cs;

cs = [[NSCharacterSet characterSetWithCharactersInString:kEnglishNum] invertedSet];

NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];

BOOL basic = [string isEqualToString:filtered];

NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];

if (_delegate && [_delegate respondsToSelector:@selector(passCard:)]) {

[_delegate passCard:text];

}

return basic;

}

return YES;


}


要求UItextField指数如中文、数字、英文

标签:

原文地址:http://blog.csdn.net/longshiqing14/article/details/45370049

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!