Skip to content

Commit 167a1a3

Browse files
j-piaseckifacebook-github-bot
authored andcommitted
Improve text measurement for multiline TextInput on iOS (facebook#49711)
Summary: Pull Request resolved: facebook#49711 Changelog: [IOS][CHANGED] - Moved workaround for multiline text measurement with `maximumNumberOfLines` earlier in the pipeline Reviewed By: huntie Differential Revision: D70314397 fbshipit-source-id: ef7dbf0c4bb3d5053328d81b7d5b8208e92ee7f5
1 parent 2b93377 commit 167a1a3

File tree

1 file changed

+12
-11
lines changed
  • packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager

1 file changed

+12
-11
lines changed

packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ - (TextMeasurement)measureNSAttributedString:(NSAttributedString *)attributedStr
4848

4949
CGSize maximumSize = CGSize{layoutConstraints.maximumSize.width, CGFLOAT_MAX};
5050

51+
// A workaround for the issue with empty line measurement:
52+
// When maximumNumberOfLines is set to N and N+1 line is empty, the returned
53+
// measurement is for N+1 lines. Adding any character to that line results
54+
// in the correct measurement.
55+
if (paragraphAttributes.maximumNumberOfLines > 0 && attributedString.length > 0 &&
56+
[[attributedString string] characterAtIndex:attributedString.length - 1] == '\n') {
57+
NSMutableAttributedString *mutableString =
58+
[[NSMutableAttributedString alloc] initWithAttributedString:attributedString];
59+
[mutableString replaceCharactersInRange:NSMakeRange(attributedString.length - 1, 1) withString:@"\n "];
60+
attributedString = mutableString;
61+
}
62+
5163
NSTextStorage *textStorage = [self _textStorageAndLayoutManagerWithAttributesString:attributedString
5264
paragraphAttributes:paragraphAttributes
5365
size:maximumSize];
@@ -232,17 +244,6 @@ - (NSTextStorage *)_textStorageAndLayoutManagerWithAttributesString:(NSAttribute
232244
layoutManager.usesFontLeading = NO;
233245
[layoutManager addTextContainer:textContainer];
234246

235-
// A workaround for the issue with empty line measurement:
236-
// When maximumNumberOfLines is set to N and N+1 line is empty, the returned
237-
// measurement is for N+1 lines. Adding any character to that line results
238-
// in the correct measurement.
239-
if (attributedString.length > 0 && [[attributedString string] characterAtIndex:attributedString.length - 1] == '\n') {
240-
NSMutableAttributedString *mutableString =
241-
[[NSMutableAttributedString alloc] initWithAttributedString:attributedString];
242-
[mutableString replaceCharactersInRange:NSMakeRange(attributedString.length - 1, 1) withString:@"\n "];
243-
attributedString = mutableString;
244-
}
245-
246247
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:attributedString];
247248

248249
RCTApplyBaselineOffset(textStorage);

0 commit comments

Comments
 (0)