Skip to content

Commit

Permalink
fix(ios): iOS18 adaptation of TextView component
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg committed Aug 2, 2024
1 parent 61891b1 commit bc7e611
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@
@end

@interface HippyUITextField : UITextField
/// iOS18's UITextInput adds an `editable` property, to avoid conflict, rename to `canEdit`
@property (nonatomic, assign) BOOL canEdit;
@property (nonatomic, assign) BOOL textWasPasted;
@property (nonatomic, weak) id<HippyUITextFieldResponseDelegate> responderDelegate;

@property (nonatomic, copy) HippyDirectEventBlock onBlur;
@property (nonatomic, copy) HippyDirectEventBlock onFocus;
@property (nonatomic, assign) BOOL editable;
@end

@interface HippyTextField : HippyBaseTextInput <UITextFieldDelegate>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ - (void)setReturnKeyType:(UIReturnKeyType) returnKeyType{
}
}

- (void)setEditable:(BOOL)editable {
_editable = editable;
[self setEnabled:editable];
- (void)setCanEdit:(BOOL)canEdit {
_canEdit = canEdit;
[self setEnabled:canEdit];
}

- (void)paste:(id)sender {
Expand Down
20 changes: 15 additions & 5 deletions renderer/native/ios/renderer/component/textinput/HippyTextView.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,35 @@
*/

#import <UIKit/UIKit.h>

#import "HippyView.h"
#import "HippyBaseTextInput.h"
#import "UIView+Hippy.h"

@protocol NativeRenderUITextViewResponseDelegate <NSObject>

/// Response delegate of HippyUITextView
@protocol HippyUITextViewResponseDelegate <NSObject>
@required
- (void)textview_becomeFirstResponder;
- (void)textview_resignFirstResponder;
@end

@interface NativeRenderUITextView : UITextView

/// UITextView's Hippy Extension
@interface HippyUITextView : UITextView
/// iOS18's UITextInput adds an `editable` property, which conflicts with the one defined in HippyUITextField.
/// For consistency, we added a canEdit property here too, which has the same meaning as editable
@property (nonatomic, assign) BOOL canEdit;
/// Indicate whether text was pasted
@property (nonatomic, assign) BOOL textWasPasted;
@property (nonatomic, weak) id<NativeRenderUITextViewResponseDelegate> responderDelegate;
/// Response delegate of HippyUITextView
@property (nonatomic, weak) id<HippyUITextViewResponseDelegate> responderDelegate;
@end


/// Hippy multi-line TextView component
@interface HippyTextView : HippyBaseTextInput <UITextViewDelegate> {
@protected
NativeRenderUITextView *_textView;
HippyUITextView *_textView;
}

@property (nonatomic, assign) BOOL autoCorrect;
Expand Down
17 changes: 11 additions & 6 deletions renderer/native/ios/renderer/component/textinput/HippyTextView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@
*/

#import "HippyTextView.h"

#import "HippyConvert.h"
#import "HippyShadowText.h"
#import "HippyText.h"
#import "HippyUtils.h"
#import "HippyTextSelection.h"
#import "UIView+Hippy.h"

@implementation NativeRenderUITextView
@implementation HippyUITextView

- (void)paste:(id)sender {
_textWasPasted = YES;
Expand Down Expand Up @@ -70,9 +69,15 @@ - (UIColor*)caretColor{
- (void)setCaretColor:(UIColor*)color{
self.tintColor = color;
}

- (void)setCanEdit:(BOOL)canEdit {
_canEdit = canEdit;
[self setEditable:canEdit];
}

@end

@interface HippyTextView () <NativeRenderUITextViewResponseDelegate>
@interface HippyTextView () <HippyUITextViewResponseDelegate>

/// ParagraphStyle for TextView and PlaceholderView,
/// used for lineHeight config and etc.
Expand Down Expand Up @@ -130,7 +135,7 @@ - (instancetype)initWithFrame:(CGRect)frame {
[self setContentInset:UIEdgeInsetsZero];
_placeholderTextColor = [self defaultPlaceholderTextColor];
_blurOnSubmit = NO;
_textView = [[NativeRenderUITextView alloc] initWithFrame:CGRectZero];
_textView = [[HippyUITextView alloc] initWithFrame:CGRectZero];
_textView.responderDelegate = self;
_textView.backgroundColor = [UIColor clearColor];
_textView.textColor = [UIColor blackColor];
Expand Down Expand Up @@ -557,7 +562,7 @@ static BOOL findMismatch(NSString *first, NSString *second, NSRange *firstRange,
return YES;
}

- (BOOL)textView:(NativeRenderUITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
- (BOOL)textView:(HippyUITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if (_onKeyPress) {
NSString *resultKey = text;
if ([text isEqualToString:@" "]) {
Expand Down Expand Up @@ -627,7 +632,7 @@ - (BOOL)textView:(NativeRenderUITextView *)textView shouldChangeTextInRange:(NSR
return YES;
}

- (void)textViewDidChangeSelection:(NativeRenderUITextView *)textView {
- (void)textViewDidChangeSelection:(HippyUITextView *)textView {
if (_onSelectionChange && textView.selectedTextRange != _previousSelectionRange
&& ![textView.selectedTextRange isEqual:_previousSelectionRange]) {
_previousSelectionRange = textView.selectedTextRange;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ - (HippyShadowView *)shadowView {
HIPPY_EXPORT_VIEW_PROPERTY(clearTextOnFocus, BOOL)
HIPPY_REMAP_VIEW_PROPERTY(color, textView.textColor, UIColor)
HIPPY_REMAP_VIEW_PROPERTY(textAlign, textView.textAlignment, NSTextAlignment)
HIPPY_REMAP_VIEW_PROPERTY(editable, textView.editable, BOOL)
HIPPY_REMAP_VIEW_PROPERTY(editable, textView.canEdit, BOOL)
HIPPY_REMAP_VIEW_PROPERTY(enablesReturnKeyAutomatically, textView.enablesReturnKeyAutomatically, BOOL)
HIPPY_REMAP_VIEW_PROPERTY(keyboardType, textView.keyboardType, UIKeyboardType)
HIPPY_REMAP_VIEW_PROPERTY(keyboardAppearance, textView.keyboardAppearance, UIKeyboardAppearance)
Expand Down

0 comments on commit bc7e611

Please sign in to comment.