1. 实现
UITextFieldDelegate 协议。
2. 在头文件中,,定义如下变量:
//限制用户只能输入字母和数字
#define kAlphaNum @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
3. 在实现文件中:
将 textFiled对象的delegate设置为self:
m_accountField = [[UITextFieldalloc] initWithFrame:CGRectMake(89,26, 243, 33)];
=UITextBorderStyleNone;
m_accountField.font = fieldFont;
m_accountField.delegate =self;
//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{NSCharacterSet *cs;cs = [[NSCharacterSet characterSetWithCharactersInString:kAlphaNum] invertedSet];NSString *filtered =[[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];BOOL basic = [string isEqualToString:filtered];return basic;}
于是夜莺会在黎明到来之前勇敢的将胸膛顶住蔷薇的刺,