Skip to content

Commit

Permalink
[iOS][TextInput] Apply the fix for CJK languages on single-line text …
Browse files Browse the repository at this point in the history
…fields.
  • Loading branch information
mandrigin committed Dec 17, 2018
1 parent 9d00d4d commit 5592d66
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Libraries/Text/TextInput/Singleline/RCTUITextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

@implementation RCTUITextField {
RCTBackedTextFieldDelegateAdapter *_textInputDelegateAdapter;
NSMutableAttributedString *_attributesHolder;
}

- (instancetype)initWithFrame:(CGRect)frame
Expand All @@ -25,6 +26,7 @@ - (instancetype)initWithFrame:(CGRect)frame
object:self];

_textInputDelegateAdapter = [[RCTBackedTextFieldDelegateAdapter alloc] initWithTextField:self];
_attributesHolder = [[NSMutableAttributedString alloc] init];
}

return self;
Expand Down Expand Up @@ -107,6 +109,37 @@ - (CGRect)caretRectForPosition:(UITextPosition *)position
return [super caretRectForPosition:position];
}

#pragma mark - Fix for CJK Languages
// Search for `GH-19339` to see all code affected by this fix.

- (void)setAttributedText:(NSAttributedString *)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.
BOOL textWasChanged = ![_attributesHolder.string isEqualToString:attributedText.string];

// We update attributes only if possible.
[_attributesHolder setAttributedString:attributedText];

// If the text has changed, there is nothing we can do.
if (textWasChanged) {
[super setAttributedText:attributedText];
}
}

- (NSAttributedString *)attributedText {
// For the text contents `super.attributedText.string` is the source of truth.
// If it differs, we match our internal state to it.
if(![super.attributedText.string isEqualToString:_attributesHolder.string]) {
[_attributesHolder setAttributedString:super.attributedText];
}

// For the text attributes, our internal state is the source of truth.
return _attributesHolder;
}

#pragma mark - Positioning Overrides

- (CGRect)textRectForBounds:(CGRect)bounds
Expand Down

0 comments on commit 5592d66

Please sign in to comment.