Skip to content

Commit

Permalink
RN: Fix Text Layout Ignoring Parent Bounds
Browse files Browse the repository at this point in the history
Summary:
Fixes text layout so that the parent bounds are correctly respected. This fixes two bugs:

- **Parent width is not respected.** This was caused by the recent change to shrink-wrap text layout.
- **Parent height is not respected.** This has always been a bug.

After this change, Android will behave like iOS.

Changelog:
[Android] [Fixed] - Text layout no longer ignores parent bounds

Reviewed By: mdvacca

Differential Revision: D21199030

fbshipit-source-id: cc072bdcff64167db1f79b7bf965e57a7396cdf4
  • Loading branch information
yungsters authored and facebook-github-bot committed Apr 24, 2020
1 parent ed29ba1 commit 025be81
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,29 @@ public long measure(
// Instead of using `layout.getWidth()` (which may yield a significantly larger width for
// text that is wrapping), compute width using the longest line.
float layoutWidth = 0;
for (int lineIndex = 0; lineIndex < lineCount; lineIndex++) {
float lineWidth = layout.getLineWidth(lineIndex);
if (lineWidth > layoutWidth) {
layoutWidth = lineWidth;
if (widthMode == YogaMeasureMode.EXACTLY) {
layoutWidth = width;
} else {
for (int lineIndex = 0; lineIndex < lineCount; lineIndex++) {
float lineWidth = layout.getLineWidth(lineIndex);
if (lineWidth > layoutWidth) {
layoutWidth = lineWidth;
}
}
if (widthMode == YogaMeasureMode.AT_MOST && layoutWidth > width) {
layoutWidth = width;
}
}

float layoutHeight = height;
if (heightMode != YogaMeasureMode.EXACTLY) {
layoutHeight = layout.getLineBottom(lineCount - 1);
if (heightMode == YogaMeasureMode.AT_MOST && layoutHeight > height) {
layoutHeight = height;
}
}

return YogaMeasureOutput.make(layoutWidth, layout.getLineBottom(lineCount - 1));
return YogaMeasureOutput.make(layoutWidth, layoutHeight);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,28 @@ public static long measureText(
? layout.getLineCount()
: Math.min(maximumNumberOfLines, layout.getLineCount());

int calculatedHeight = layout.getLineBottom(calculatedLineCount - 1);
// Instead of using `layout.getWidth()` (which may yield a significantly larger width for
// text that is wrapping), compute width using the longest line.
int calculatedWidth = 0;
for (int lineIndex = 0; lineIndex < calculatedLineCount; lineIndex++) {
float lineWidth = layout.getLineWidth(lineIndex);
if (lineWidth > calculatedWidth) {
calculatedWidth = (int) Math.ceil(lineWidth);
float calculatedWidth = 0;
if (widthYogaMeasureMode == YogaMeasureMode.EXACTLY) {
calculatedWidth = width;
} else {
for (int lineIndex = 0; lineIndex < calculatedLineCount; lineIndex++) {
float lineWidth = layout.getLineWidth(lineIndex);
if (lineWidth > calculatedWidth) {
calculatedWidth = lineWidth;
}
}
if (widthYogaMeasureMode == YogaMeasureMode.AT_MOST && calculatedWidth > width) {
calculatedWidth = width;
}
}

float calculatedHeight = height;
if (heightYogaMeasureMode != YogaMeasureMode.EXACTLY) {
calculatedHeight = layout.getLineBottom(calculatedLineCount - 1);
if (heightYogaMeasureMode == YogaMeasureMode.AT_MOST && calculatedHeight > height) {
calculatedHeight = height;
}
}

Expand Down Expand Up @@ -356,7 +370,7 @@ public static long measureText(
isRtlParagraph
// Equivalent to `layout.getLineLeft(line)` but `getLineLeft` returns incorrect
// values when the paragraph is RTL and `setSingleLine(true)`.
? calculatedWidth - (int) layout.getLineWidth(line)
? (int) calculatedWidth - (int) layout.getLineWidth(line)
: (int) layout.getLineRight(line) - placeholderWidth;
} else {
// The direction of the paragraph may not be exactly the direction the string is heading
Expand All @@ -379,7 +393,8 @@ public static long measureText(
// The result is equivalent to bugless versions of
// `getPrimaryHorizontal`/`getSecondaryHorizontal`.
placeholderLeftPosition =
calculatedWidth - ((int) layout.getLineRight(line) - placeholderLeftPosition);
(int) calculatedWidth
- ((int) layout.getLineRight(line) - placeholderLeftPosition);
}
if (isRtlChar) {
placeholderLeftPosition -= placeholderWidth;
Expand Down

0 comments on commit 025be81

Please sign in to comment.