标签:
关于iOS的文本框有时需要限制字数,如手机号,在UITextField的代理单纯写一个判断,在字数超过限制时,这时再想删除就删除不掉,可以在代理这样写,就解决
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (!string.length) { return YES; } if (textField.keyboardType == UIKeyboardTypeNumberPad) { if ([string rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]].location != NSNotFound) { return NO; } } NSString *updatedText = [textField.text stringByReplacingCharactersInRange:range withString:string]; if (updatedText.length > 11) // 4 was chosen for SSN verification { if (string.length > 1) { // BasicAlert(@"", @"This field accepts a maximum of 4 characters."); } return NO; } return YES; }
标签:
原文地址:http://www.cnblogs.com/mangoes/p/4434760.html