From 3d8e3c725338378d264ec5e00c58a1130bd1d85e Mon Sep 17 00:00:00 2001 From: magicien Date: Fri, 9 Mar 2018 01:46:32 +0900 Subject: [PATCH 1/4] Add text property to RCTBaseTextInputView - Add text VIEW_PROPERTY to RCTBaseTextInputViewManager.m - Add text accessor to RCTBaseTextInputView.m --- Libraries/Text/TextInput/RCTBaseTextInputView.m | 10 ++++++++++ Libraries/Text/TextInput/RCTBaseTextInputViewManager.m | 1 + 2 files changed, 11 insertions(+) diff --git a/Libraries/Text/TextInput/RCTBaseTextInputView.m b/Libraries/Text/TextInput/RCTBaseTextInputView.m index d9c47ff16311d4..e3dffecd584681 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputView.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputView.m @@ -93,6 +93,16 @@ - (void)setReactBorderInsets:(UIEdgeInsets)reactBorderInsets [self setNeedsLayout]; } +- (NSString *)text +{ + return [self attributedText].string; +} + +- (void)setText:(NSString *)text +{ + [self setAttributedText:[[NSAttributedString alloc] initWithString: text]]; +} + - (NSAttributedString *)attributedText { return self.backedTextInputView.attributedText; diff --git a/Libraries/Text/TextInput/RCTBaseTextInputViewManager.m b/Libraries/Text/TextInput/RCTBaseTextInputViewManager.m index aaead20b0b9c1c..efb6fa64f757ec 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputViewManager.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputViewManager.m @@ -60,6 +60,7 @@ @implementation RCTBaseTextInputViewManager RCT_EXPORT_VIEW_PROPERTY(onSelectionChange, RCTDirectEventBlock) RCT_EXPORT_VIEW_PROPERTY(onTextInput, RCTDirectEventBlock) RCT_EXPORT_VIEW_PROPERTY(onScroll, RCTDirectEventBlock) +RCT_EXPORT_VIEW_PROPERTY(text, NSString) RCT_EXPORT_VIEW_PROPERTY(mostRecentEventCount, NSInteger) From 01440a8fef0262774d5429403608da4b47e7769e Mon Sep 17 00:00:00 2001 From: Cristiano Santos Date: Tue, 13 Mar 2018 17:58:15 +0000 Subject: [PATCH 2/4] Reapply attributes of NSAttributedString on the setText method --- Libraries/Text/TextInput/RCTBaseTextInputView.m | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Libraries/Text/TextInput/RCTBaseTextInputView.m b/Libraries/Text/TextInput/RCTBaseTextInputView.m index e3dffecd584681..39bc78baadb12b 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputView.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputView.m @@ -100,7 +100,19 @@ - (NSString *)text - (void)setText:(NSString *)text { - [self setAttributedText:[[NSAttributedString alloc] initWithString: text]]; + if (self.attributedText.length > 0) { + //copy the attributes + NSRange range = NSMakeRange(self.attributedText.length-1, self.attributedText.length); + NSDictionary *attributes = [self.attributedText attributesAtIndex:0 effectiveRange:&range]; + NSMutableAttributedString *newString = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes]; + NSMutableAttributedString *primaryStringMutable = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText]; + + //change the string + [primaryStringMutable setAttributedString:newString]; + [self setAttributedText:[[NSAttributedString alloc] initWithAttributedString:primaryStringMutable]]; + } else { + [self setAttributedText:[[NSAttributedString alloc] initWithString: text]]; + } } - (NSAttributedString *)attributedText From 1dcb8763c6c263f6673ec8f0cd04be39b3c3e94a Mon Sep 17 00:00:00 2001 From: magicien Date: Tue, 20 Mar 2018 02:14:15 +0900 Subject: [PATCH 3/4] Revise: change "text" property handling - Remove text property from RCTBaseTextInputView - Add text property accessor to RCTBaseTextInputShadowView --- .../TextInput/RCTBaseTextInputShadowView.m | 12 ++++++++++ .../Text/TextInput/RCTBaseTextInputView.m | 22 ------------------- .../TextInput/RCTBaseTextInputViewManager.m | 1 - 3 files changed, 12 insertions(+), 23 deletions(-) diff --git a/Libraries/Text/TextInput/RCTBaseTextInputShadowView.m b/Libraries/Text/TextInput/RCTBaseTextInputShadowView.m index d1fdbcd3d25012..cf1155b9aa308a 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputShadowView.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputShadowView.m @@ -23,6 +23,7 @@ @implementation RCTBaseTextInputShadowView NSAttributedString *_Nullable _localAttributedText; CGSize _previousContentSize; + NSString *_text; NSTextStorage *_textStorage; NSTextContainer *_textContainer; NSLayoutManager *_layoutManager; @@ -101,6 +102,17 @@ - (void)invalidateContentSize }); } +- (NSString *)text +{ + return _text; +} + +- (void)setText:(NSString *)text +{ + _text = text; + _previousAttributedText = _localAttributedText; +} + #pragma mark - RCTUIManagerObserver - (void)uiManagerWillPerformMounting diff --git a/Libraries/Text/TextInput/RCTBaseTextInputView.m b/Libraries/Text/TextInput/RCTBaseTextInputView.m index 39bc78baadb12b..d9c47ff16311d4 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputView.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputView.m @@ -93,28 +93,6 @@ - (void)setReactBorderInsets:(UIEdgeInsets)reactBorderInsets [self setNeedsLayout]; } -- (NSString *)text -{ - return [self attributedText].string; -} - -- (void)setText:(NSString *)text -{ - if (self.attributedText.length > 0) { - //copy the attributes - NSRange range = NSMakeRange(self.attributedText.length-1, self.attributedText.length); - NSDictionary *attributes = [self.attributedText attributesAtIndex:0 effectiveRange:&range]; - NSMutableAttributedString *newString = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes]; - NSMutableAttributedString *primaryStringMutable = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText]; - - //change the string - [primaryStringMutable setAttributedString:newString]; - [self setAttributedText:[[NSAttributedString alloc] initWithAttributedString:primaryStringMutable]]; - } else { - [self setAttributedText:[[NSAttributedString alloc] initWithString: text]]; - } -} - - (NSAttributedString *)attributedText { return self.backedTextInputView.attributedText; diff --git a/Libraries/Text/TextInput/RCTBaseTextInputViewManager.m b/Libraries/Text/TextInput/RCTBaseTextInputViewManager.m index efb6fa64f757ec..aaead20b0b9c1c 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputViewManager.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputViewManager.m @@ -60,7 +60,6 @@ @implementation RCTBaseTextInputViewManager RCT_EXPORT_VIEW_PROPERTY(onSelectionChange, RCTDirectEventBlock) RCT_EXPORT_VIEW_PROPERTY(onTextInput, RCTDirectEventBlock) RCT_EXPORT_VIEW_PROPERTY(onScroll, RCTDirectEventBlock) -RCT_EXPORT_VIEW_PROPERTY(text, NSString) RCT_EXPORT_VIEW_PROPERTY(mostRecentEventCount, NSInteger) From a374a6afeec7928e4164a8e1a5ad98064227c169 Mon Sep 17 00:00:00 2001 From: magicien Date: Sun, 19 Aug 2018 23:14:55 +0900 Subject: [PATCH 4/4] Revise: Update setText func of RCTBaseTextInputShadowView - Set nil to _previousAttributedText instead of _localAttributedText - Call dirtyLayout after changing `text` value --- Libraries/Text/TextInput/RCTBaseTextInputShadowView.m | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Libraries/Text/TextInput/RCTBaseTextInputShadowView.m b/Libraries/Text/TextInput/RCTBaseTextInputShadowView.m index cf1155b9aa308a..154f63633a0213 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputShadowView.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputShadowView.m @@ -110,7 +110,10 @@ - (NSString *)text - (void)setText:(NSString *)text { _text = text; - _previousAttributedText = _localAttributedText; + // Clear `_previousAttributedText` to notify the view about the change + // when `text` native prop is set. + _previousAttributedText = nil; + [self dirtyLayout]; } #pragma mark - RCTUIManagerObserver