diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java index c5e232cf734..3d420f8b9ea 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java @@ -707,6 +707,10 @@ public class TextLayoutManager { for (int lineIndex = 0; lineIndex < calculatedLineCount; lineIndex++) { boolean endsWithNewLine = text.length() > 0 && text.charAt(layout.getLineEnd(lineIndex) - 1) == '\n'; + if (!endsWithNewLine && lineIndex + 1 < layout.getLineCount()) { + calculatedWidth = width; + break; + } float lineWidth = endsWithNewLine ? layout.getLineMax(lineIndex) : layout.getLineWidth(lineIndex); if (lineWidth > calculatedWidth) { diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm index 8b00c5f8e2b..cc080f5257a 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm @@ -347,8 +347,33 @@ static NSLineBreakMode RCTNSLineBreakModeFromEllipsizeMode(EllipsizeMode ellipsi NSTextContainer *textContainer = layoutManager.textContainers.firstObject; [layoutManager ensureLayoutForTextContainer:textContainer]; + NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer]; + __block BOOL textDidWrap = NO; + [layoutManager + enumerateLineFragmentsForGlyphRange:glyphRange + usingBlock:^( + CGRect overallRect, + CGRect usedRect, + NSTextContainer *_Nonnull usedTextContainer, + NSRange lineGlyphRange, + BOOL *_Nonnull stop) { + NSRange range = [layoutManager characterRangeForGlyphRange:lineGlyphRange + actualGlyphRange:nil]; + NSUInteger lastCharacterIndex = range.location + range.length - 1; + BOOL endsWithNewLine = + [textStorage.string characterAtIndex:lastCharacterIndex] == '\n'; + if (!endsWithNewLine && textStorage.string.length > lastCharacterIndex + 1) { + textDidWrap = YES; + *stop = YES; + } + }]; + CGSize size = [layoutManager usedRectForTextContainer:textContainer].size; + if (textDidWrap) { + size.width = textContainer.size.width; + } + size = (CGSize){RCTCeilPixelValue(size.width), RCTCeilPixelValue(size.height)}; __block auto attachments = TextMeasurement::Attachments{};