From ab370f3f84d0fc1c19705d09f402461e73ffb842 Mon Sep 17 00:00:00 2001 From: magicien Date: Wed, 14 Mar 2018 10:55:23 +0900 Subject: [PATCH] Apply #18278 to fix TextInput.clear() bug --- .../Text/TextInput/RCTBaseTextInputView.m | 22 +++++++++++++++++++ .../TextInput/RCTBaseTextInputViewManager.m | 1 + 2 files changed, 23 insertions(+) diff --git a/Libraries/Text/TextInput/RCTBaseTextInputView.m b/Libraries/Text/TextInput/RCTBaseTextInputView.m index d9c47ff16311d4..39bc78baadb12b 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputView.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputView.m @@ -93,6 +93,28 @@ - (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 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)