如何更好地限制一个uitextfield键盘类型的输入长度

objective-c textField限制只能输入一定长度的字符 - 为程序员服务
为程序员服务
textField限制只能输入一定长度的字符
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)
//string就是此时输入的那个字符textField就是此时正在输入的那个输入框返回YES就是可以改变输入框的值NO相反
if ([string isEqualToString:@&\\n&])
//按会车可以改变
return YES;
NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容
if (self.myTextField == textField)
//判断是否时我们想要限定的那个输入框
if ([toBeString length] & 20) { //如果输入框内容大于20则弹出警告
textField.text = [toBeString substringToIndex:20];
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:nil message:@&超过最大字数不能输入了& delegate:nil cancelButtonTitle:@&Ok& otherButtonTitles:nil, nil] autorelease];
[alert show];
return NO;
return YES;
//该片段来自于
您可能的代码
相关聚客文章
相关专栏文章如何限制UITextField的输入长度_百度知道
如何限制UITextField的输入长度
提问者采纳
要限制一个UITextField的输入字数(参考链接),首先想到的应该是通过UITextFieldDelegate的代理方法来限制:- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)
// return NO to not change text比如要设置字数限制为20:- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if (textField == self.titleField) {
if (textField.length & 20) return NO;
return YES;
其他类似问题
为您推荐:
uitextfield的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁如何限制UITextField的输入长度_百度知道
如何限制UITextField的输入长度
提问者采纳
要限制一个UITextField的输入字数,首先想到的应该是通过UITextFieldDelegate的代理方法来限制:- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)
// return NO to not change text比如要设置字数限制为20:- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if (textField == self.titleField) {
if (textField.length & 20) return NO;
return YES;Answer your question:1. For password ,
you should change the property
check the secure box.2 For less than 12 character you can use - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// return NO to not change text
if(strlen([textField.text UTF8String]) &= 10 && range.length != 1)
return NO;
return YES;----------------------------------------------------------------------------------------------------------------}3. for more than 6 you can use - (BOOL)textFieldShouldReturn:(UITextField *)textField{
// return NO to not change text
if(strlen([textField.text UTF8String]) &6)
//UIAlertView tmpAlert = [[UIAlertView alloc] initWithTitle:@&Wrong password& message:@&Password can not less than 6 character& delegate:self cancelButtonTitle:@&Cancel& otherButtonTitles:nil];
// [tmpAlert release];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@&Wrong password&
message:@&Password can not less than 6 character&
delegate:self
cancelButtonTitle:@&Go on Input&
otherButtonTitles:nil];
[alert show];
[alert release];
return NO;
[self.text resignFirstResponder];
return YES;}
来自团队:
其他类似问题
为您推荐:
uitextfield的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁如何限制UITextField的输入长度_百度知道
如何限制UITextField的输入长度
提问者采纳
Answer your question:1. For password ,
you should change the property
check the secure box.2 For less than 12 character you can use - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// return NO to not change text
if(strlen([textField.text UTF8String]) &= 10 && range.length != 1)
return NO;
return YES;}3. for more than 6 you can use - (BOOL)textFieldShouldReturn:(UITextField *)textField{
// return NO to not change text
if(strlen([textField.text UTF8String]) &6)
//UIAlertView tmpAlert = [[UIAlertView alloc] initWithTitle:@&Wrong password& message:@&Password can not less than 6 character& delegate:self cancelButtonTitle:@&Cancel& otherButtonTitles:nil];
// [tmpAlert release];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@&Wrong password&
message:@&Password can not less than 6 character&
delegate:self
cancelButtonTitle:@&Go on Input&
otherButtonTitles:nil];
[alert show];
[alert release];
return NO;
[self.text resignFirstResponder];
return YES;}
资深电脑人
其他类似问题
为您推荐:
uitextfield的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 uitextfield 的文章

 

随机推荐