diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/FontMetricsUtil.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/FontMetricsUtil.java index 84dc328edab3af..0cd632bd5b100f 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/FontMetricsUtil.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/FontMetricsUtil.java @@ -42,8 +42,14 @@ public static WritableArray getFontMetrics( X_HEIGHT_MEASUREMENT_TEXT, 0, X_HEIGHT_MEASUREMENT_TEXT.length(), xHeightBounds); double xHeight = xHeightBounds.height() / AMPLIFICATION_FACTOR / dm.density; for (int i = 0; i < layout.getLineCount(); i++) { - boolean endsWithNewLine = text.length() > 0 && text.charAt(layout.getLineEnd(i) - 1) == '\n'; - float lineWidth = endsWithNewLine ? layout.getLineMax(i) : layout.getLineWidth(i); + float lineWidth; + if (layout.getLineCount() == 1) { + lineWidth = layout.getEllipsizedWidth(); + } else { + boolean endsWithNewLine = + text.length() > 0 && text.charAt(layout.getLineEnd(i) - 1) == '\n'; + lineWidth = endsWithNewLine ? layout.getLineMax(i) : layout.getLineWidth(i); + } Rect bounds = new Rect(); layout.getLineBounds(i, bounds); WritableMap line = Arguments.createMap(); diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextShadowNode.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextShadowNode.java index be27e77d13956a..c0f22686bd1bef 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextShadowNode.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextShadowNode.java @@ -135,13 +135,17 @@ public long measure( if (widthMode == YogaMeasureMode.EXACTLY) { layoutWidth = width; } else { - for (int lineIndex = 0; lineIndex < lineCount; lineIndex++) { - boolean endsWithNewLine = - text.length() > 0 && text.charAt(layout.getLineEnd(lineIndex) - 1) == '\n'; - float lineWidth = - endsWithNewLine ? layout.getLineMax(lineIndex) : layout.getLineWidth(lineIndex); - if (lineWidth > layoutWidth) { - layoutWidth = lineWidth; + if (mNumberOfLines == 1) { + layoutWidth = (int) layout.getEllipsizedWidth(); + } else { + for (int lineIndex = 0; lineIndex < lineCount; lineIndex++) { + boolean endsWithNewLine = + text.length() > 0 && text.charAt(layout.getLineEnd(lineIndex) - 1) == '\n'; + float lineWidth = + endsWithNewLine ? layout.getLineMax(lineIndex) : layout.getLineWidth(lineIndex); + if (lineWidth > layoutWidth) { + layoutWidth = lineWidth; + } } } if (widthMode == YogaMeasureMode.AT_MOST && layoutWidth > width) { 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 2b1928d8c41122..2ff1d3f5794b96 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 @@ -601,13 +601,17 @@ public static long measureText( if (widthYogaMeasureMode == YogaMeasureMode.EXACTLY) { calculatedWidth = width; } else { - for (int lineIndex = 0; lineIndex < calculatedLineCount; lineIndex++) { - boolean endsWithNewLine = - text.length() > 0 && text.charAt(layout.getLineEnd(lineIndex) - 1) == '\n'; - float lineWidth = - endsWithNewLine ? layout.getLineMax(lineIndex) : layout.getLineWidth(lineIndex); - if (lineWidth > calculatedWidth) { - calculatedWidth = lineWidth; + if (maximumNumberOfLines == 1) { + calculatedWidth = (int) layout.getEllipsizedWidth(); + } else { + for (int lineIndex = 0; lineIndex < calculatedLineCount; lineIndex++) { + boolean endsWithNewLine = + text.length() > 0 && text.charAt(layout.getLineEnd(lineIndex) - 1) == '\n'; + float lineWidth = + endsWithNewLine ? layout.getLineMax(lineIndex) : layout.getLineWidth(lineIndex); + if (lineWidth > calculatedWidth) { + calculatedWidth = lineWidth; + } } } if (widthYogaMeasureMode == YogaMeasureMode.AT_MOST && calculatedWidth > width) { @@ -660,14 +664,18 @@ public static long measureText( // the last offset in the layout will result in an endless loop. Work around // this bug by avoiding getPrimaryHorizontal in that case. if (start == text.length() - 1) { - boolean endsWithNewLine = - text.length() > 0 && text.charAt(layout.getLineEnd(line) - 1) == '\n'; - float lineWidth = endsWithNewLine ? layout.getLineMax(line) : layout.getLineWidth(line); + float lineWidth; + if (maximumNumberOfLines == 1) { + lineWidth = layout.getEllipsizedWidth(); + } else { + boolean endsWithNewLine = + text.length() > 0 && text.charAt(layout.getLineEnd(line) - 1) == '\n'; + lineWidth = endsWithNewLine ? layout.getLineMax(line) : layout.getLineWidth(line); + } placeholderLeftPosition = isRtlParagraph // Equivalent to `layout.getLineLeft(line)` but `getLineLeft` returns - // incorrect - // values when the paragraph is RTL and `setSingleLine(true)`. + // incorrect values when the paragraph is RTL and `setSingleLine(true)`. ? calculatedWidth - lineWidth : layout.getLineRight(line) - placeholderWidth; } else {