Skip to content

Commit dd492a1

Browse files
committed
Manually cherry pick updated changes from facebook#35703
1 parent 2d5847c commit dd492a1

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private void initView() {
8787

8888
mReactBackgroundManager = new ReactViewBackgroundManager(this);
8989

90-
mNumberOfLines = ViewDefaults.NUMBER_OF_LINES;
90+
mNumberOfLines = ViewDefaults.MAXIMUM_NUMBER_OF_LINES;
9191
mAdjustsFontSizeToFit = false;
9292
mLinkifyMaskType = 0;
9393
mNotifyOnInlineViewLayout = false;
@@ -573,7 +573,7 @@ public boolean hasOverlappingRendering() {
573573
}
574574

575575
public void setNumberOfLines(int numberOfLines) {
576-
mNumberOfLines = numberOfLines == 0 ? ViewDefaults.NUMBER_OF_LINES : numberOfLines;
576+
mNumberOfLines = numberOfLines == 0 ? ViewDefaults.MAXIMUM_NUMBER_OF_LINES : numberOfLines;
577577
setSingleLine(mNumberOfLines == 1);
578578
setMaxLines(mNumberOfLines);
579579
}
@@ -593,7 +593,7 @@ public void setNotifyOnInlineViewLayout(boolean notifyOnInlineViewLayout) {
593593
public void updateView() {
594594
@Nullable
595595
TextUtils.TruncateAt ellipsizeLocation =
596-
mNumberOfLines == ViewDefaults.NUMBER_OF_LINES || mAdjustsFontSizeToFit
596+
mNumberOfLines == ViewDefaults.MAXIMUM_NUMBER_OF_LINES || mAdjustsFontSizeToFit
597597
? null
598598
: mEllipsizeLocation;
599599
setEllipsize(ellipsizeLocation);

packages/react-native/ReactCommon/react/renderer/components/textinput/androidtextinput/react/renderer/components/androidtextinput/AndroidTextInputProps.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ void AndroidTextInputProps::setProp(
335335
}
336336

337337
switch (hash) {
338+
RAW_SET_PROP_SWITCH_CASE_BASIC(maximumNumberOfLines);
338339
RAW_SET_PROP_SWITCH_CASE_BASIC(autoComplete);
339340
RAW_SET_PROP_SWITCH_CASE_BASIC(returnKeyLabel);
340341
RAW_SET_PROP_SWITCH_CASE_BASIC(numberOfLines);

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

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -244,25 +244,48 @@ - (void)getRectWithAttributedString:(AttributedString)attributedString
244244

245245
#pragma mark - Private
246246

247-
- (NSTextStorage *)_textStorageForNSAttributesString:(NSAttributedString *)attributedString
247+
- (NSTextStorage *)_textStorageForNSAttributesString:(NSAttributedString *)inputAttributedString
248248
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
249249
size:(CGSize)size
250250
{
251-
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:size];
251+
NSMutableAttributedString *attributedString = [ inputAttributedString mutableCopy];
252+
253+
/*
254+
* The block below is responsible for setting the exact height of the view in lines
255+
* Unfortunatelly, iOS doesn't export any easy way to do it. So we set maximumNumberOfLines
256+
* prop and then add random lines at the front. However, they are only used for layout
257+
* so they are not visible on the screen. This method is used for drawing only for Paragraph component
258+
* but we set exact height in lines only on TextInput that doesn't use it.
259+
*/
260+
if (paragraphAttributes.numberOfLines) {
261+
paragraphAttributes.maximumNumberOfLines = paragraphAttributes.numberOfLines;
262+
NSMutableString *newLines = [NSMutableString stringWithCapacity: paragraphAttributes.numberOfLines];
263+
for (NSUInteger i = 0UL; i < paragraphAttributes.numberOfLines; ++i) {
264+
// K is added on purpose. New line seems to be not enough for NTtextContainer
265+
[newLines appendString:@"K\n"];
266+
}
267+
NSDictionary<NSAttributedStringKey, id> * attributesOfFirstCharacter = [inputAttributedString attributesAtIndex:0 effectiveRange:NULL];
252268

253-
textContainer.lineFragmentPadding = 0.0; // Note, the default value is 5.
254-
textContainer.lineBreakMode = paragraphAttributes.maximumNumberOfLines > 0
255-
? RCTNSLineBreakModeFromEllipsizeMode(paragraphAttributes.ellipsizeMode)
256-
: NSLineBreakByClipping;
257-
textContainer.maximumNumberOfLines = paragraphAttributes.maximumNumberOfLines;
269+
270+
[attributedString insertAttributedString:[[NSAttributedString alloc] initWithString:newLines attributes:attributesOfFirstCharacter] atIndex:0];
271+
}
272+
273+
NSTextContainer *textContainer = [NSTextContainer new];
258274

259275
NSLayoutManager *layoutManager = [NSLayoutManager new];
260276
layoutManager.usesFontLeading = NO;
261277
[layoutManager addTextContainer:textContainer];
278+
NSTextStorage *textStorage = [NSTextStorage new];
279+
[textStorage addLayoutManager:layoutManager];
262280

263-
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:attributedString];
281+
textContainer.lineFragmentPadding = 0.0; // Note, the default value is 5.
282+
textContainer.lineBreakMode = paragraphAttributes.maximumNumberOfLines > 0
283+
? RCTNSLineBreakModeFromEllipsizeMode(paragraphAttributes.ellipsizeMode)
284+
: NSLineBreakByClipping;
285+
textContainer.size = size;
286+
textContainer.maximumNumberOfLines = paragraphAttributes.maximumNumberOfLines;
264287

265-
[textStorage addLayoutManager:layoutManager];
288+
[textStorage replaceCharactersInRange:(NSRange){0, textStorage.length} withAttributedString:attributedString];
266289

267290
if (paragraphAttributes.adjustsFontSizeToFit) {
268291
CGFloat minimumFontSize = !isnan(paragraphAttributes.minimumFontSize) ? paragraphAttributes.minimumFontSize : 4.0;

0 commit comments

Comments
 (0)