Skip to content
This repository has been archived by the owner on Dec 3, 2021. It is now read-only.

Commit

Permalink
Fixes complex input on iOS in RN 0.55.x.
Browse files Browse the repository at this point in the history
The fix is described here: facebook#19809
  • Loading branch information
rasom authored and yenda committed Nov 29, 2018
1 parent 0b6d361 commit bcf5f6f
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion Libraries/Text/TextInput/Multiline/RCTUITextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,21 @@ - (void)setTextAlignment:(NSTextAlignment)textAlignment

- (void)setAttributedText:(NSAttributedString *)attributedText
{
[super setAttributedText:attributedText];
// Using `setAttributedString:` while user is typing breaks some internal mechanics
// when entering complex input languages such as Chinese, Korean or Japanese.
// see: https://github.com/facebook/react-native/issues/19339

// We try to avoid calling this method as much as we can.
// If the text has changed, there is nothing we can do.
if (![super.attributedText.string isEqualToString:attributedText.string]) {
[super setAttributedText:attributedText];
} else {
// But if the text is preserved, we just copying the attributes from the source string.
if (![super.attributedText isEqualToAttributedString:attributedText]) {
[self copyTextAttributesFrom:attributedText];
}
}

[self textDidChange];
}

Expand Down Expand Up @@ -241,4 +255,20 @@ - (void)invalidatePlaceholderVisibility
_placeholderView.hidden = !isVisible;
}

#pragma mark - Utility Methods

- (void)copyTextAttributesFrom:(NSAttributedString *)sourceString
{
[self.textStorage beginEditing];

NSTextStorage *textStorage = self.textStorage;
[sourceString enumerateAttributesInRange:NSMakeRange(0, sourceString.length)
options:NSAttributedStringEnumerationReverse
usingBlock:^(NSDictionary<NSAttributedStringKey,id> * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) {
[textStorage setAttributes:attrs range:range];
}];

[self.textStorage endEditing];
}

@end

0 comments on commit bcf5f6f

Please sign in to comment.