Apply the fix for CJK languages on single-line text fields. (#22546)

Summary:
Follow-up to https://github.com/facebook/react-native/pull/19809

This fix generalizes the `setAttributedString:` fix to single-line text fields.

Fixes #19339

_Pull requests that expand test coverage are more likely to get reviewed. Add a test case whenever possible!_
Pull Request resolved: https://github.com/facebook/react-native/pull/22546

Differential Revision: D13948951

Pulled By: shergin

fbshipit-source-id: 67992c02b32f33f6d61fac4554e4f46b973262c1
This commit is contained in:
Igor Mandrigin 2019-02-04 15:00:58 -08:00 committed by Facebook Github Bot
parent 8ae185280d
commit 05ebf77175
1 changed files with 45 additions and 0 deletions

View File

@ -14,6 +14,7 @@
@implementation RCTUITextField {
RCTBackedTextFieldDelegateAdapter *_textInputDelegateAdapter;
NSMutableAttributedString *_attributesHolder;
}
- (instancetype)initWithFrame:(CGRect)frame
@ -25,6 +26,7 @@
object:self];
_textInputDelegateAdapter = [[RCTBackedTextFieldDelegateAdapter alloc] initWithTextField:self];
_attributesHolder = [[NSMutableAttributedString alloc] init];
}
return self;
@ -107,6 +109,49 @@
return [super caretRectForPosition:position];
}
#pragma mark - Fix for CJK Languages
/*
* The workaround to fix inputting complex locales (like CJK languages).
* When we use `setAttrbutedText:` while user is inputting text in a complex
* locale (like Chinese, Japanese or Korean), some internal state breaks and
* input stops working.
*
* To workaround that, we don't skip underlying attributedString in the text
* field if only attributes were changed. We keep track of these attributes in
* a local variable.
*
* There are two methods that are altered by this workaround:
*
* (1) `-setAttributedText:`
* Applies the attributed string change to a local variable `_attributesHolder` instead of calling `-[super setAttributedText:]`.
* If new attributed text differs from the existing one only in attributes,
* skips `-[super setAttributedText:`] completely.
*
* (2) `-attributedText`
* Return `_attributesHolder` context.
* Updates `_atributesHolder` before returning if the underlying `super.attributedText.string` was changed.
*
*/
- (void)setAttributedText:(NSAttributedString *)attributedText
{
BOOL textWasChanged = ![_attributesHolder.string isEqualToString:attributedText.string];
[_attributesHolder setAttributedString:attributedText];
if (textWasChanged) {
[super setAttributedText:attributedText];
}
}
- (NSAttributedString *)attributedText
{
if (![super.attributedText.string isEqualToString:_attributesHolder.string]) {
[_attributesHolder setAttributedString:super.attributedText];
}
return _attributesHolder;
}
#pragma mark - Positioning Overrides
- (CGRect)textRectForBounds:(CGRect)bounds