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 4 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
57 changes: 40 additions & 17 deletions Libraries/Text/TextInput/RCTBaseTextInputView.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#import "RCTInputAccessoryViewContent.h"
#import "RCTTextAttributes.h"
#import "RCTTextSelection.h"
#import "NSString+RCTUtility.h"

@implementation RCTBaseTextInputView {
__weak RCTBridge *_bridge;
Expand Down Expand Up @@ -373,24 +374,14 @@ - (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;
// Get allowedLength based on glyphs
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) {
// 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];

if (textGlyphsLength > 1) {
[self trimInputTextInRange:range replacementText:text allowedLength:allowedLength];
[self textInputDidChange];
}

Expand Down Expand Up @@ -619,6 +610,38 @@ - (void)handleInputAccessoryDoneButton

#pragma mark - Helpers

- (void)trimInputTextInRange:(NSRange)range
Copy link
Contributor

Choose a reason for hiding this comment

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

Not exactly.
This separate function is still a method (it mutates self) which does not solve the complexity problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@shergin Yeah, actually, Swift already solved this glyphs issue, but OC, we seems need to do these heavy things by ourself. Have no idea how to move on. 😂

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, Swift's strings are character-aware but normal Objective-C are not. Yeah... but that's only String's properties, that is not related to UITextField and UITextView implementation, right? So, we already have two subclasses for those classes that enhance the behaviors and fix some issue. Maybe we can encapsulate that character-aware logic there?

Or, what if we move that maxLength feature implementation into those classes and try to make it totally transparent for all external logic?

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;
_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];
}

static BOOL findMismatch(NSString *first, NSString *second, NSRange *firstRange, NSRange *secondRange)
{
NSInteger firstMismatch = -1;
Expand Down