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
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions Libraries/Text/NSString+RCTUtility.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSString (RCTUtility)
@property (nonatomic, readonly) NSUInteger reactLengthOfGlyphs;
@end

NS_ASSUME_NONNULL_END
21 changes: 21 additions & 0 deletions Libraries/Text/NSString+RCTUtility.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#import "NSString+RCTUtility.h"

@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
12 changes: 12 additions & 0 deletions Libraries/Text/RCTText.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
objects = {

/* Begin PBXBuildFile section */
0E8D88DB224B1515000FE6B8 /* NSString+RCTUtility.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E8D88DA224B1515000FE6B8 /* NSString+RCTUtility.m */; };
0E8D88DC224B153B000FE6B8 /* NSString+RCTUtility.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E8D88DA224B1515000FE6B8 /* NSString+RCTUtility.m */; };
0E8D88DD224B154A000FE6B8 /* NSString+RCTUtility.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 0E8D88D9224B1515000FE6B8 /* NSString+RCTUtility.h */; };
0E8D88DE224B1573000FE6B8 /* NSString+RCTUtility.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 0E8D88D9224B1515000FE6B8 /* NSString+RCTUtility.h */; };
5956B130200FEBAA008D9D16 /* RCTRawTextShadowView.m in Sources */ = {isa = PBXBuildFile; fileRef = 5956B0FD200FEBA9008D9D16 /* RCTRawTextShadowView.m */; };
5956B131200FEBAA008D9D16 /* RCTRawTextViewManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 5956B0FE200FEBA9008D9D16 /* RCTRawTextViewManager.m */; };
5956B132200FEBAA008D9D16 /* RCTSinglelineTextInputView.m in Sources */ = {isa = PBXBuildFile; fileRef = 5956B101200FEBA9008D9D16 /* RCTSinglelineTextInputView.m */; };
Expand Down Expand Up @@ -139,6 +143,7 @@
5956B171200FF324008D9D16 /* RCTBaseTextInputView.h in Copy Headers */,
5956B172200FF324008D9D16 /* RCTBaseTextInputViewManager.h in Copy Headers */,
5956B173200FF324008D9D16 /* RCTTextSelection.h in Copy Headers */,
0E8D88DD224B154A000FE6B8 /* NSString+RCTUtility.h in Copy Headers */,
5956B174200FF324008D9D16 /* RCTSinglelineTextInputView.h in Copy Headers */,
5956B175200FF324008D9D16 /* RCTSinglelineTextInputViewManager.h in Copy Headers */,
5956B176200FF324008D9D16 /* RCTUITextField.h in Copy Headers */,
Expand All @@ -157,6 +162,7 @@
5970936A20845F0600D163A7 /* RCTTextTransform.h in Copy Headers */,
5956B179200FF338008D9D16 /* RCTBaseTextShadowView.h in Copy Headers */,
5956B17A200FF338008D9D16 /* RCTBaseTextViewManager.h in Copy Headers */,
0E8D88DE224B1573000FE6B8 /* NSString+RCTUtility.h in Copy Headers */,
5956B17B200FF338008D9D16 /* RCTRawTextShadowView.h in Copy Headers */,
5956B17C200FF338008D9D16 /* RCTRawTextViewManager.h in Copy Headers */,
5956B17D200FF338008D9D16 /* RCTConvert+Text.h in Copy Headers */,
Expand Down Expand Up @@ -187,6 +193,8 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
0E8D88D9224B1515000FE6B8 /* NSString+RCTUtility.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSString+RCTUtility.h"; sourceTree = "<group>"; };
0E8D88DA224B1515000FE6B8 /* NSString+RCTUtility.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSString+RCTUtility.m"; sourceTree = "<group>"; };
2D2A287B1D9B048500D4039D /* libRCTText-tvOS.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libRCTText-tvOS.a"; sourceTree = BUILT_PRODUCTS_DIR; };
58B5119B1A9E6C1200147676 /* libRCTText.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRCTText.a; sourceTree = BUILT_PRODUCTS_DIR; };
5956B0F9200FEBA9008D9D16 /* RCTConvert+Text.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTConvert+Text.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -260,6 +268,8 @@
5956B11A200FEBA9008D9D16 /* RCTTextAttributes.h */,
5956B120200FEBA9008D9D16 /* RCTTextAttributes.m */,
5970936820845DDE00D163A7 /* RCTTextTransform.h */,
0E8D88D9224B1515000FE6B8 /* NSString+RCTUtility.h */,
0E8D88DA224B1515000FE6B8 /* NSString+RCTUtility.m */,
5956B121200FEBAA008D9D16 /* Text */,
5956B0FF200FEBA9008D9D16 /* TextInput */,
5956B12A200FEBAA008D9D16 /* VirtualText */,
Expand Down Expand Up @@ -457,6 +467,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
0E8D88DC224B153B000FE6B8 /* NSString+RCTUtility.m in Sources */,
5956B192200FF35C008D9D16 /* RCTBaseTextShadowView.m in Sources */,
5956B193200FF35C008D9D16 /* RCTBaseTextViewManager.m in Sources */,
5956B194200FF35C008D9D16 /* RCTRawTextShadowView.m in Sources */,
Expand Down Expand Up @@ -487,6 +498,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
0E8D88DB224B1515000FE6B8 /* NSString+RCTUtility.m in Sources */,
5956B13D200FEBAA008D9D16 /* RCTBaseTextShadowView.m in Sources */,
5956B140200FEBAA008D9D16 /* RCTTextShadowView.m in Sources */,
5956B131200FEBAA008D9D16 /* RCTRawTextViewManager.m in Sources */,
Expand Down
55 changes: 55 additions & 0 deletions Libraries/Text/TextInput/Multiline/RCTMultilineTextInputView.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import <React/RCTUtils.h>

#import "RCTUITextView.h"
#import "NSString+RCTUtility.h"

@implementation RCTMultilineTextInputView
{
Expand Down Expand Up @@ -45,6 +46,60 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge
return _backedTextInputView;
}

- (BOOL)handleTextInputLengthLimitInRange:(NSRange)range
replacementText:(NSString *)text
{
id<RCTBackedTextInputViewProtocol> backedTextInputView = self.backedTextInputView;
if (self.maxLength) {
NSString *backedTextInputViewText = backedTextInputView.attributedText.string;
// Get allowedLength based on glyphs
NSInteger allowedLength = MAX(self.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 (textGlyphsLength > 1) {
[self trimInputTextInRange:range replacementText:text allowedLength:allowedLength];
[self textInputDidChange];
}

return YES;
}
}
return NO;
}

- (void)trimInputTextInRange:(NSRange)range
replacementText:(NSString *)text
allowedLength:(NSInteger)length
{
__block NSUInteger allowedIndex = 0;
__block NSInteger allowedLength = length;

id<RCTBackedTextInputViewProtocol> backedTextInputView = self.backedTextInputView;

// 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:allowedIndex];
NSMutableAttributedString *newAttributedText = [backedTextInputView.attributedText mutableCopy];
[newAttributedText replaceCharactersInRange:range withString:limitedString];
backedTextInputView.attributedText = newAttributedText;
self.predictedText = newAttributedText.string;

// Collapse selection at end of insert to match normal paste behavior.
UITextPosition *insertEnd = [backedTextInputView positionFromPosition:backedTextInputView.beginningOfDocument
offset:(range.location + limitedString.length)];
[backedTextInputView setSelectedTextRange:[backedTextInputView textRangeFromPosition:insertEnd toPosition:insertEnd]
notifyDelegate:YES];
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
Expand Down
1 change: 1 addition & 0 deletions Libraries/Text/TextInput/RCTBaseTextInputView.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, copy) NSAttributedString *attributedText;
@property (nonatomic, copy) NSString *inputAccessoryViewID;
@property (nonatomic, assign) UIKeyboardType keyboardType;
@property (nonatomic, copy, nullable) NSString *predictedText;

@end

Expand Down
35 changes: 10 additions & 25 deletions Libraries/Text/TextInput/RCTBaseTextInputView.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ @implementation RCTBaseTextInputView {
__weak RCTBridge *_bridge;
__weak RCTEventDispatcher *_eventDispatcher;
BOOL _hasInputAccesoryView;
NSString *_Nullable _predictedText;
NSInteger _nativeEventCount;
}

Expand Down Expand Up @@ -372,30 +371,9 @@ - (BOOL)textInputShouldChangeTextInRange:(NSRange)range replacementText:(NSStrin
eventCount:_nativeEventCount];
}

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

if (text.length > allowedLength) {
// If we typed/pasted more than one character, limit the text inputted.
if (text.length > 1) {
// Truncate the input string so the result is exactly maxLength
NSString *limitedString = [text substringToIndex:allowedLength];
NSMutableAttributedString *newAttributedText = [backedTextInputView.attributedText mutableCopy];
[newAttributedText replaceCharactersInRange:range withString:limitedString];
backedTextInputView.attributedText = newAttributedText;
_predictedText = newAttributedText.string;

// Collapse selection at end of insert to match normal paste behavior.
UITextPosition *insertEnd = [backedTextInputView positionFromPosition:backedTextInputView.beginningOfDocument
offset:(range.location + allowedLength)];
[backedTextInputView setSelectedTextRange:[backedTextInputView textRangeFromPosition:insertEnd toPosition:insertEnd]
notifyDelegate:YES];

[self textInputDidChange];
}

return NO;
}
BOOL isLimited = [self handleTextInputLengthLimitInRange:range replacementText:text];
if (isLimited) {
return NO;
}

NSString *previousText = backedTextInputView.attributedText.string ?: @"";
Expand Down Expand Up @@ -619,6 +597,13 @@ - (void)handleInputAccessoryDoneButton

#pragma mark - Helpers

- (BOOL)handleTextInputLengthLimitInRange:(NSRange)range
replacementText:(NSString *)text
{
RCTAssert(NO, @"-[RCTBaseTextInputView handleTextInputLengthLimitInRange:replacementText:] must be implemented in subclass.");
return YES;
}

static BOOL findMismatch(NSString *first, NSString *second, NSRange *firstRange, NSRange *secondRange)
{
NSInteger firstMismatch = -1;
Expand Down
55 changes: 55 additions & 0 deletions Libraries/Text/TextInput/Singleline/RCTSinglelineTextInputView.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import <React/RCTBridge.h>

#import "RCTUITextField.h"
#import "NSString+RCTUtility.h"

@implementation RCTSinglelineTextInputView
{
Expand Down Expand Up @@ -37,4 +38,58 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge
return _backedTextInputView;
}

- (BOOL)handleTextInputLengthLimitInRange:(NSRange)range
replacementText:(NSString *)text
{
id<RCTBackedTextInputViewProtocol> backedTextInputView = self.backedTextInputView;
if (self.maxLength) {
NSString *backedTextInputViewText = backedTextInputView.attributedText.string;
// Get allowedLength based on glyphs
NSInteger allowedLength = MAX(self.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 (textGlyphsLength > 1) {
[self trimInputTextInRange:range replacementText:text allowedLength:allowedLength];
[self textInputDidChange];
}

return YES;
}
}
return NO;
}

- (void)trimInputTextInRange:(NSRange)range
replacementText:(NSString *)text
allowedLength:(NSInteger)length
{
__block NSUInteger allowedIndex = 0;
__block NSInteger allowedLength = length;

id<RCTBackedTextInputViewProtocol> backedTextInputView = self.backedTextInputView;

// 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:allowedIndex];
NSMutableAttributedString *newAttributedText = [backedTextInputView.attributedText mutableCopy];
[newAttributedText replaceCharactersInRange:range withString:limitedString];
backedTextInputView.attributedText = newAttributedText;
self.predictedText = newAttributedText.string;

// Collapse selection at end of insert to match normal paste behavior.
UITextPosition *insertEnd = [backedTextInputView positionFromPosition:backedTextInputView.beginningOfDocument
offset:(range.location + limitedString.length)];
[backedTextInputView setSelectedTextRange:[backedTextInputView textRangeFromPosition:insertEnd toPosition:insertEnd]
notifyDelegate:YES];
}

@end