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] Fixed maxLength of TextInput based on glyph count #24109

Closed
wants to merge 6 commits into from
Closed
Changes from 1 commit
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
40 changes: 35 additions & 5 deletions Libraries/Text/TextInput/RCTBaseTextInputView.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@
#import "RCTTextAttributes.h"
#import "RCTTextSelection.h"

@interface NSString (RCTUtility)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to decouple that into separate file(s).


@property (nonatomic, readonly) NSUInteger reactLengthOfGlyphs;

@end

@implementation NSString (RCTUtility)

- (NSUInteger)reactLengthOfGlyphs
{
__block NSUInteger lengthOfGlyphs = 0;
[self enumerateSubstringsInRange:NSMakeRange(0, self.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop){
lengthOfGlyphs++;
}];
return lengthOfGlyphs;
}

@end

@implementation RCTBaseTextInputView {
__weak RCTBridge *_bridge;
__weak RCTEventDispatcher *_eventDispatcher;
Expand Down Expand Up @@ -373,13 +392,24 @@ - (BOOL)textInputShouldChangeTextInRange:(NSRange)range replacementText:(NSStrin
}

if (_maxLength) {
NSInteger allowedLength = MAX(_maxLength.integerValue - (NSInteger)backedTextInputView.attributedText.string.length + (NSInteger)range.length, 0);

if (text.length > allowedLength) {
NSString *backedTextInputViewText = backedTextInputView.attributedText.string;
__block NSInteger allowedLength = MAX(_maxLength.integerValue - (NSInteger)backedTextInputViewText.reactLengthOfGlyphs + (NSInteger)[backedTextInputViewText substringWithRange:range].reactLengthOfGlyphs, 0);
NSUInteger textGlyphsLength = text.reactLengthOfGlyphs;
if (textGlyphsLength > allowedLength) {
// If we typed/pasted more than one character, limit the text inputted.
if (text.length > 1) {
if (textGlyphsLength > 1) {
__block NSUInteger allowedIndex = 0;
// We truncated the text based on glyphs.
[text enumerateSubstringsInRange:NSMakeRange(0, text.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop){
if (allowedLength == 0) {
*stop = YES;
return;
}
allowedIndex = substringRange.location + substringRange.length;
allowedLength--;
}];
// Truncate the input string so the result is exactly maxLength
NSString *limitedString = [text substringToIndex:allowedLength];
NSString *limitedString = [text substringToIndex:allowedIndex];
NSMutableAttributedString *newAttributedText = [backedTextInputView.attributedText mutableCopy];
[newAttributedText replaceCharactersInRange:range withString:limitedString];
backedTextInputView.attributedText = newAttributedText;
Expand Down