From 24e5439529451e0033dec829cf98486c59cb49e2 Mon Sep 17 00:00:00 2001 From: wwwcg Date: Thu, 12 Sep 2024 20:02:52 +0800 Subject: [PATCH] fix(ios): keyboard related events not working for TextView (#4026) add missing onKeyboardHeightChanged property --- .../component/textinput/HippyBaseTextInput.h | 9 ++- .../component/textinput/HippyBaseTextInput.m | 56 +++++++++++++++++++ .../component/textinput/HippyTextField.h | 2 - .../component/textinput/HippyTextField.m | 18 ------ .../component/textinput/HippyTextView.h | 2 - .../component/textinput/HippyTextView.mm | 21 ------- .../textinput/HippyTextViewManager.mm | 14 +---- 7 files changed, 65 insertions(+), 57 deletions(-) diff --git a/renderer/native/ios/renderer/component/textinput/HippyBaseTextInput.h b/renderer/native/ios/renderer/component/textinput/HippyBaseTextInput.h index 3651f027112..24816e20704 100644 --- a/renderer/native/ios/renderer/component/textinput/HippyBaseTextInput.h +++ b/renderer/native/ios/renderer/component/textinput/HippyBaseTextInput.h @@ -28,10 +28,15 @@ @property (nonatomic, assign) UIEdgeInsets contentInset; @property (nonatomic, copy) NSString *value; +/// Keyboard will show event +@property (nonatomic, copy) HippyDirectEventBlock onKeyboardWillShow; +/// Keyboard will hide event +@property (nonatomic, copy) HippyDirectEventBlock onKeyboardWillHide; +/// Keyboard height change event +@property (nonatomic, copy) HippyDirectEventBlock onKeyboardHeightChanged; + - (void)focus; - (void)blur; - (void)clearText; -- (void)keyboardWillShow:(NSNotification *)aNotification; -- (void)keyboardWillHide:(NSNotification *)aNotification; @end diff --git a/renderer/native/ios/renderer/component/textinput/HippyBaseTextInput.m b/renderer/native/ios/renderer/component/textinput/HippyBaseTextInput.m index 88af7f79dbe..3559c79205e 100644 --- a/renderer/native/ios/renderer/component/textinput/HippyBaseTextInput.m +++ b/renderer/native/ios/renderer/component/textinput/HippyBaseTextInput.m @@ -22,7 +22,10 @@ #import "HippyBaseTextInput.h" +static NSString *const kKeyboardHeightKey = @"keyboardHeight"; + @implementation HippyBaseTextInput + - (void)focus { // base method, should be override } @@ -32,11 +35,64 @@ - (void)blur { - (void)clearText { // base method, should be override } + - (void)keyboardWillShow:(NSNotification *)aNotification { // base method, should be override + NSDictionary *userInfo = [aNotification userInfo]; + NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; + CGRect keyboardRect = [aValue CGRectValue]; + CGFloat keyboardHeight = keyboardRect.size.height; + if (self.isFirstResponder && self.onKeyboardWillShow) { + self.onKeyboardWillShow(@{ kKeyboardHeightKey : @(keyboardHeight) }); + } } + - (void)keyboardWillHide:(NSNotification *)aNotification { // base method, should be override + if (self.onKeyboardWillHide) { + self.onKeyboardWillHide(@{}); + } +} + +- (void)keyboardHeightChanged:(NSNotification *)aNotification { + // base method, should be override + NSDictionary *userInfo = [aNotification userInfo]; + NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; + CGRect keyboardRect = [aValue CGRectValue]; + CGFloat keyboardHeight = keyboardRect.size.height; + if (self.isFirstResponder && self.onKeyboardHeightChanged) { + self.onKeyboardHeightChanged(@{ kKeyboardHeightKey : @(keyboardHeight) }); + } +} + +- (void)setOnKeyboardWillShow:(HippyDirectEventBlock)onKeyboardWillShow { + if (_onKeyboardWillShow != onKeyboardWillShow) { + _onKeyboardWillShow = [onKeyboardWillShow copy]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(keyboardWillShow:) + name:UIKeyboardWillShowNotification + object:nil]; + } +} + +- (void)setOnKeyboardWillHide:(HippyDirectEventBlock)onKeyboardWillHide { + if (_onKeyboardWillHide != onKeyboardWillHide) { + _onKeyboardWillHide = [onKeyboardWillHide copy]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(keyboardWillHide:) + name:UIKeyboardWillHideNotification + object:nil]; + } +} + +- (void)setOnKeyboardHeightChanged:(HippyDirectEventBlock)onKeyboardHeightChanged { + if (_onKeyboardHeightChanged != onKeyboardHeightChanged) { + _onKeyboardHeightChanged = [onKeyboardHeightChanged copy]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(keyboardHeightChanged:) + name:UIKeyboardWillChangeFrameNotification + object:nil]; + } } @end diff --git a/renderer/native/ios/renderer/component/textinput/HippyTextField.h b/renderer/native/ios/renderer/component/textinput/HippyTextField.h index acb957fc9d3..4976993eb63 100644 --- a/renderer/native/ios/renderer/component/textinput/HippyTextField.h +++ b/renderer/native/ios/renderer/component/textinput/HippyTextField.h @@ -58,8 +58,6 @@ @property (nonatomic, copy) HippyDirectEventBlock onBlur; @property (nonatomic, copy) HippyDirectEventBlock onFocus; @property (nonatomic, copy) HippyDirectEventBlock onEndEditing; -@property (nonatomic, copy) HippyDirectEventBlock onKeyboardWillShow; -@property (nonatomic, copy) HippyDirectEventBlock onKeyboardWillHide; @property (nonatomic, copy) NSString *value; @property (nonatomic, strong) NSString *defaultValue; diff --git a/renderer/native/ios/renderer/component/textinput/HippyTextField.m b/renderer/native/ios/renderer/component/textinput/HippyTextField.m index eaffcf17335..7bca6def9ae 100644 --- a/renderer/native/ios/renderer/component/textinput/HippyTextField.m +++ b/renderer/native/ios/renderer/component/textinput/HippyTextField.m @@ -123,24 +123,6 @@ @implementation HippyTextField { @dynamic lineSpacing; @dynamic lineHeightMultiple; -- (void)keyboardWillShow:(NSNotification *)aNotification { - [super keyboardWillShow:aNotification]; - NSDictionary *userInfo = [aNotification userInfo]; - NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; - CGRect keyboardRect = [aValue CGRectValue]; - CGFloat keyboardHeight = keyboardRect.size.height; - if (_textView.isFirstResponder && _onKeyboardWillShow) { - _onKeyboardWillShow(@{ @"keyboardHeight": @(keyboardHeight) }); - } -} - -- (void)keyboardWillHide:(NSNotification *)aNotification { - [super keyboardWillHide:aNotification]; - if (_onKeyboardWillHide) { - _onKeyboardWillHide(@{}); - } -} - - (instancetype)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { [self setContentInset:UIEdgeInsetsZero]; diff --git a/renderer/native/ios/renderer/component/textinput/HippyTextView.h b/renderer/native/ios/renderer/component/textinput/HippyTextView.h index 0a287acd2f9..044ff02ac97 100644 --- a/renderer/native/ios/renderer/component/textinput/HippyTextView.h +++ b/renderer/native/ios/renderer/component/textinput/HippyTextView.h @@ -83,8 +83,6 @@ @property (nonatomic, copy) HippyDirectEventBlock onChangeText; @property (nonatomic, copy) HippyDirectEventBlock onBlur; @property (nonatomic, copy) HippyDirectEventBlock onFocus; -@property (nonatomic, copy) HippyDirectEventBlock onKeyboardWillShow; -@property (nonatomic, copy) HippyDirectEventBlock onKeyboardWillHide; - (void)updateFrames; @end diff --git a/renderer/native/ios/renderer/component/textinput/HippyTextView.mm b/renderer/native/ios/renderer/component/textinput/HippyTextView.mm index 95c6fbdff9f..0e34e9886ec 100644 --- a/renderer/native/ios/renderer/component/textinput/HippyTextView.mm +++ b/renderer/native/ios/renderer/component/textinput/HippyTextView.mm @@ -110,27 +110,6 @@ @implementation HippyTextView { @dynamic lineSpacing; @dynamic lineHeightMultiple; -#pragma mark - Keyboard Events - -- (void)keyboardWillShow:(NSNotification *)aNotification { - [super keyboardWillShow:aNotification]; - //获取键盘的高度 - NSDictionary *userInfo = [aNotification userInfo]; - NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; - CGRect keyboardRect = [aValue CGRectValue]; - CGFloat keyboardHeight = keyboardRect.size.height; - if (self.isFirstResponder && _onKeyboardWillShow) { - _onKeyboardWillShow(@{ @"keyboardHeight": @(keyboardHeight) }); - } -} - -- (void)keyboardWillHide:(NSNotification *)aNotification { - [super keyboardWillHide:aNotification]; - if (_onKeyboardWillHide) { - _onKeyboardWillHide(@{}); - } -} - - (instancetype)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { [self setContentInset:UIEdgeInsetsZero]; diff --git a/renderer/native/ios/renderer/component/textinput/HippyTextViewManager.mm b/renderer/native/ios/renderer/component/textinput/HippyTextViewManager.mm index 3842dd9777c..518d46d6c61 100644 --- a/renderer/native/ios/renderer/component/textinput/HippyTextViewManager.mm +++ b/renderer/native/ios/renderer/component/textinput/HippyTextViewManager.mm @@ -47,18 +47,6 @@ - (UIView *)view { } else { theView = [[HippyTextView alloc] init]; } - if (self.props[@"onKeyboardWillShow"]) { - [[NSNotificationCenter defaultCenter] addObserver:theView - selector:@selector(keyboardWillShow:) - name:UIKeyboardWillShowNotification - object:nil]; - } - if (self.props[@"onKeyboardWillHide"]) { - [[NSNotificationCenter defaultCenter] addObserver:theView - selector:@selector(keyboardWillHide:) - name:UIKeyboardWillHideNotification - object:nil]; - } return theView; } @@ -72,6 +60,8 @@ - (HippyShadowView *)shadowView { HIPPY_EXPORT_VIEW_PROPERTY(onBlur, HippyDirectEventBlock) HIPPY_EXPORT_VIEW_PROPERTY(onFocus, HippyDirectEventBlock) HIPPY_EXPORT_VIEW_PROPERTY(onKeyboardWillShow, HippyDirectEventBlock) +HIPPY_EXPORT_VIEW_PROPERTY(onKeyboardWillHide, HippyDirectEventBlock) +HIPPY_EXPORT_VIEW_PROPERTY(onKeyboardHeightChanged, HippyDirectEventBlock) HIPPY_EXPORT_VIEW_PROPERTY(defaultValue, NSString) HIPPY_EXPORT_VIEW_PROPERTY(isNightMode, BOOL)