iOS 限制TextField输入长度
1
#import "MyInfoEditTableViewCell.h" @interface MyInfoEditTableViewCell()<UITextFieldDelegate> @property (weak, nonatomic) IBOutlet UITextField *editField; @end @implementation MyInfoEditTableViewCell - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (textField == self.editField) { //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果. if (range.length == 1 && string.length == 0) { return YES; } else if (self.editField.text.length >= 30) { self.editField.text = [textField.text substringToIndex:30]; return NO; } } return YES; } - (void)awakeFromNib { [super awakeFromNib]; self.editField.delegate = self; } @end