Skip to content

Commit

Permalink
fix(ios): keyboard related events not working for TextView (#4026)
Browse files Browse the repository at this point in the history
add missing onKeyboardHeightChanged property
  • Loading branch information
wwwcg authored Sep 12, 2024
1 parent 7ec9c24 commit 24e5439
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@

#import "HippyBaseTextInput.h"

static NSString *const kKeyboardHeightKey = @"keyboardHeight";

@implementation HippyBaseTextInput

- (void)focus {
// base method, should be override
}
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 0 additions & 18 deletions renderer/native/ios/renderer/component/textinput/HippyTextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 0 additions & 21 deletions renderer/native/ios/renderer/component/textinput/HippyTextView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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)

Expand Down

0 comments on commit 24e5439

Please sign in to comment.