Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS][TextInput] Apply the fix for CJK languages on single-line text fields. #22546

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 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,49 @@ - (CGRect)caretRectForPosition:(UITextPosition *)position
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];

This comment was marked as outdated.

This comment was marked as outdated.


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
Expand Down