首页 > 学院 > 开发设计 > 正文

UITextField实时跟踪输入文字的个数,超过禁止输入并阶段(UITextView也可以使用类似方法)

2019-11-09 18:14:02
字体:
来源:转载
供稿:网友

UITextField

#PRagma mark - UITextFieldDelegate- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{    if(textField == self.nameTextField){        UITextRange *selectedRange = [textField markedTextRange];        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];        if(position && (range.length==0 || string.length==0)){            return YES;        }else{            NSString *str = [textField.text stringByReplacingCharactersInRange:NSMakeRange(range.location, range.length) withString:string];            if(str.length>30){//限制字数,多余截断                self.nameTextField.text = [str substringToIndex:30];                [self.dict setObject:self.nameTextField.text forKey:@"name"]//保存到dict中                if(self.guestParamEdit){                    self.guestParamEdit(self.dict, self);                }                return NO;            }else{//保存到dict中                [self.dict setObject:str forKey:@"name"];                if(self.guestParamEdit){                    self.guestParamEdit(self.dict, self);                }            }        }    }    return YES;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表