iOS: fix when input set maxLength, delete text in middel, the selection move to end

This commit is contained in:
pengfei.zhou 2022-03-24 18:11:59 +08:00 committed by osborn
parent 421df45638
commit a87fb84260

View File

@ -578,7 +578,9 @@ - (void)textViewDidChange:(UITextView *)textView {
if (textView.markedTextRange) return; if (textView.markedTextRange) return;
if (self.maxLength) { if (self.maxLength) {
UITextRange *range = textView.selectedTextRange;
textView.text = [self limitToHansMaxLength:self.maxLength.unsignedIntValue text:textView.text]; textView.text = [self limitToHansMaxLength:self.maxLength.unsignedIntValue text:textView.text];
textView.selectedTextRange = range;
} }
if (self.onTextChange) { if (self.onTextChange) {
self.onTextChange(textView.text, self); self.onTextChange(textView.text, self);
@ -611,7 +613,9 @@ - (NSString *)limitToHansMaxLength:(NSUInteger)maxLen text:(NSString *)text {
- (void)textFieldDidChange:(UITextField *)textField { - (void)textFieldDidChange:(UITextField *)textField {
if (self.maxLength) { if (self.maxLength) {
UITextRange *range = textField.selectedTextRange;
textField.text = [self limitToHansMaxLength:self.maxLength.unsignedIntValue text:textField.text]; textField.text = [self limitToHansMaxLength:self.maxLength.unsignedIntValue text:textField.text];
textField.selectedTextRange = range;
} }
if (self.onTextChange) { if (self.onTextChange) {
self.onTextChange(textField.text, self); self.onTextChange(textField.text, self);