iOS: input focus fix

This commit is contained in:
pengfei.zhou 2021-09-09 14:13:22 +08:00 committed by osborn
parent abc39ed28f
commit 145e157558

View File

@ -153,6 +153,17 @@ - (void)setMultiline:(BOOL)multiline {
if (multiline == self.multiline) { if (multiline == self.multiline) {
return; return;
} }
if (multiline) {
self.multiLineInput.text = self.singleLineInput.text;
if (self.singleLineInput.isFirstResponder) {
[self.multiLineInput becomeFirstResponder];
}
} else {
self.singleLineInput.text = self.multiLineInput.text;
if (self.multiLineInput.isFirstResponder) {
[self.singleLineInput becomeFirstResponder];
}
}
self.singleLineInput.hidden = multiline; self.singleLineInput.hidden = multiline;
self.multiLineInput.hidden = !multiline; self.multiLineInput.hidden = !multiline;
} }
@ -310,6 +321,7 @@ @implementation DoricInputNode
- (DoricInputView *)build { - (DoricInputView *)build {
DoricInputView *v = [DoricInputView new]; DoricInputView *v = [DoricInputView new];
v.singleLineInput.delegate = self; v.singleLineInput.delegate = self;
[v.singleLineInput addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
v.multiLineInput.delegate = self; v.multiLineInput.delegate = self;
v.multiLineInput.textContainer.lineFragmentPadding = 0; v.multiLineInput.textContainer.lineFragmentPadding = 0;
v.doricLayout.paddingTop = v.multiLineInput.textContainerInset.top; v.doricLayout.paddingTop = v.multiLineInput.textContainerInset.top;
@ -518,27 +530,33 @@ - (NSDictionary *)getSelection {
} }
- (void)requestFocus { - (void)requestFocus {
[self.view becomeFirstResponder]; if (self.view.multiline) {
[self.view.multiLineInput becomeFirstResponder];
} else {
[self.view.singleLineInput becomeFirstResponder];
}
} }
- (void)releaseFocus { - (void)releaseFocus {
[self.view resignFirstResponder]; if (self.view.multiline) {
[self.view.multiLineInput resignFirstResponder];
} else {
[self.view.singleLineInput resignFirstResponder];
}
} }
#pragma mark - UITextViewDelegate #pragma mark - UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView { - (void)textViewDidBeginEditing:(UITextView *)textView {
if (self.onFocusChange) { if (self.onFocusChange) {
self.onFocusChange(YES, self); self.onFocusChange(YES, self);
} }
return YES;
} }
- (BOOL)textViewShouldEndEditing:(UITextView *)textView { - (void)textViewDidEndEditing:(UITextView *)textView {
if (self.onFocusChange) { if (self.onFocusChange) {
self.onFocusChange(NO, self); self.onFocusChange(NO, self);
} }
return YES;
} }
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
@ -600,13 +618,8 @@ - (NSString *)limitToHansMaxLength:(NSUInteger)maxLen text:(NSString *)text {
return [text substringWithRange:NSMakeRange(0, subStringRangeLen)]; return [text substringWithRange:NSMakeRange(0, subStringRangeLen)];
} }
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (!textField.window.isKeyWindow) {
[textField.window makeKeyAndVisible];
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField { - (void)textFieldDidChange:(UITextField *)textField {
if (self.maxLength) { if (self.maxLength) {
textField.text = [self limitToHansMaxLength:self.maxLength.unsignedIntValue text:textField.text]; textField.text = [self limitToHansMaxLength:self.maxLength.unsignedIntValue text:textField.text];
} }
@ -616,6 +629,18 @@ - (void)textFieldDidEndEditing:(UITextField *)textField {
[textField setNeedsLayout]; [textField setNeedsLayout];
} }
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (self.onFocusChange) {
self.onFocusChange(YES, self);
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
if (self.onFocusChange) {
self.onFocusChange(NO, self);
}
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (self.beforeTextChangeFuncId) { if (self.beforeTextChangeFuncId) {
DoricAsyncResult *asyncResult = [self DoricAsyncResult *asyncResult = [self