如果要限制UITextField输入长度最长不超过kMaxLength,那么需要实现做以下操作:
1、实现UITextFieldDelegate协议;
2、实现textField:shouldChangeCharactersInRange:replacementString:方法;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSInteger strLength = textField.text.length - range.length + string.length;
return (strLength <= kMaxLength);
}
方法- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
功能:
把textField中位置为range的字符串替换为string字符串;
此函数在textField内容被修改时调用;
返回值:
YES,表示修改生效;NO,表示不做修改,textField的内容不变。
参数说明:
textField:响应UITextFieldDelegate协议的UITextField控件。
range: UITextField控件中光标选中的字符串,即被替换的字符串;
range.length为0时,表示在位置range.location插入string。
string: 替换字符串;
string.length为0时,表示删除。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
strLength = textField.text.length - range.length + string.length;
if (strLength >= TextMaxLength && string.length >= range.length) {
// 判断是否为删除字符,如果为删除则让执行
char c = [string UTF8String][0];
if (c == '\000') {
return YES;
}
if([[textField text] length] >= TextMaxLength) {
if(![string isEqualToString:@"\b"])
return NO;
}
}
return YES;
}
#pragma mark 排除由于联想输入,会有很多字符一起输入以及排除emoji表情
-(void)handleTextFieldDidChange:(NSNotification *)notification {
UITextField *textField = (UITextField *)notification.object;
NSString *toBeString = textField.text;
[textField setText:[self disable_emoji:toBeString]];
NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写
UITextRange *selectedRange = [textField markedTextRange];
//获取高亮部分
UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
// 没有高亮选择的字,则对已输入的文字进行字数统计和限制
if (!position) {
if (toBeString.length > TextMaxLength) {
textField.text = [toBeString substringToIndex:TextMaxLength];
}
}
// 有高亮选择的字符串,则暂不对文字进行统计和限制
else{
}
}
// 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
else{
if (toBeString.length > TextMaxLength) {
textField.text = [toBeString substringToIndex:TextMaxLength];
}
}
}
error: NSMutableRLEArray replaceObjectsInRange:withObject:length:: Out of bounds
实现textView的代理
- (void)textViewDidChange:(UITextView *)textView
{
// !isEmtpy && >maxLength
if(![NSString isEmpty:textView.text.trim] && textView.text.length >= TextMaxLength){
NSString *temp = [textView.text substringToIndex:TextMaxLength];
textView.text = temp;
}
self.txtFollow.text = textView.text;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
// 回车
if ([text isEqualToString:@"\n"]) {
// 禁止字符判断
NSString *jinzhi = [NSString stringWithFormat:@".*禁止.*"];
NSRange jinzhiRange = [self.txtFollow.text rangeOfString:kh1 options:NSRegularExpressionSearch];
if (rkh1.location != NSNotFound) {
NSLog(@“禁止使用”);
}
else {
[self.navigationController popViewControllerAnimated:YES];
}
}
if (range.location >= TextMaxLength) {
return NO;
}
return YES;
}