UITextField 限制輸入字數 ,推薦方法二:ci
方法一字符串
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{string
if (range.location>= 11)it
return NO;io
returnYES;方法
}字符
方法二return
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{location
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