IOS 开发之UITextField限制字数的示例分析
更新:HHH   时间:2023-1-8


这篇文章给大家分享的是有关IOS 开发之UITextField限制字数的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

IOS 开发之 UITextField限制字数的方法

在输入东西的时候,如果想限制最大字数,可以用下面方法:

实例代码:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;  
{  
  if ([string isEqualToString:@"\n"])   
  {  
    return YES;  
  }  
  NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];  
    
  if (self.myTextField == textField)   
  {  
    if ([toBeString length] > 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;  
}

感谢各位的阅读!关于“IOS 开发之UITextField限制字数的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

返回移动开发教程...