方法一
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if (range.location>= 11)
return NO;
returnYES;
}
方法二
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if (toBeString.length > 11) {
textField.text = [toBeString substringToIndex:11];
return NO;
}
returnYES;
}
当前光标位置 range.location
已选文字长度 range.length
输入文字长度 textView.text.length
已有文字长度 text.length
新闻热点
疑难解答