利用TextFieldDelegate代理方法 – textField:shouldChangeCharactersInRange:replacementString: 限制输入字符
2014-08-14 13:25 PingKang 阅读(442) 评论(0) 编辑 收藏 举报
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == _workIdTxt) {
NSUInteger lengthOfString = string.length;
for (NSInteger loopIndex = 0; loopIndex < lengthOfString; loopIndex++) {//只允许数字输入
unichar character = [string characterAtIndex:loopIndex];
if (character < 48) return NO; // 48 unichar for 0
if (character > 57) return NO; // 57 unichar for 9
}
// Check for total length
NSUInteger proposedNewLength = textField.text.length - range.length + string.length;
if (proposedNewLength > 10 || proposedNewLength<2) return NO;//限制长度
return YES;
}
return YES;
}