首先我们要设置一下键盘类型
textFiled.keyboardType = UIKeyboardTypeASCIICapable;
(根据个人喜好设置键盘)
然后我们要设置textfield的代理<UITextFieldDelegate>
设置好代理就开始写键盘了
先来定义几个宏定义
1
2
3
|
#define NUM @ "0123456789" #define ALPHA @ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" #define ALPHANUM @ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" |
然后写代理方法
1
2
3
4
5
6
|
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ALPHANUM] invertedSet]; NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@ "" ]; return [string isEqualToString:filtered]; } |
注:需要给哪个textfield设置键盘,就给哪个textfield设置代理即可
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持服务器之家!
原文链接:http://www.jianshu.com/p/255a0b8756ba