方法一
- (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
新聞熱點
疑難解答