Skip to content

Fix Android onTextLayout reporting extra lines when width is fractional#56322

Open
dexical30 wants to merge 1 commit intofacebook:mainfrom
dexical30:fix/ontextlayout-lines-count-issue
Open

Fix Android onTextLayout reporting extra lines when width is fractional#56322
dexical30 wants to merge 1 commit intofacebook:mainfrom
dexical30:fix/ontextlayout-lines-count-issue

Conversation

@dexical30
Copy link
Copy Markdown

@dexical30 dexical30 commented Apr 4, 2026

Hi, I found a bug while investigating onTextLayout behavior on Android and would love any feedback on the approach.


onTextLayout incorrectly reports a single visual line as multiple lines on Android when <Text> is placed next to a flexGrow: 1 sibling.

<View style={{ flexDirection: 'row' }}>
  <View style={{ flexGrow: 1 }} />
  <Text onTextLayout={e => console.log(e.nativeEvent.lines)}>
    Welcome!
  </Text>
</View>

// Before: lines → [{text: "Welcome"}, {text: "!"}]  ← 2 lines (incorrect)
// After:  lines → [{text: "Welcome!"}]               ← 1 line  (correct)

What I Found

After digging into the code, I believe the root cause is in TextLayoutManager.kt. When onTextLayout is present, measureLines() is called (via JNI from FabricUIManager) and creates a separate StaticLayout purely for measurement — independent of the one TextView uses for rendering.

In the pre-Android 15 path, EXACTLY mode computes the StaticLayout width using floor(width):

// TextLayoutManager.kt line 633 (before fix)
YogaMeasureMode.EXACTLY -> floor(width).toInt()

When width (the Yoga-allocated pixel value) has a fractional part — which is common on non-integer density devices like Pixel 4 (density=2.75) or Galaxy series (density=3.5) — floor() produces a StaticLayout that is 1px narrower than what TextView actually renders with.

This causes the two StaticLayouts to disagree on line breaks:

Yoga allocated width = 240.6px

  measureLines StaticLayout  →  floor(240.6) = 240px  →  "Welcome" / "!"  (2 lines)
  TextView rendering         →  getMeasuredWidth() = 241px  →  "Welcome!"  (1 line)

onTextLayout fires from the measureLines StaticLayout
→ reports 2 lines even though the text visually renders as 1 line

The flexGrow: 1 sibling seems to amplify this because Yoga's flex distribution applies Math.round() when allocating pixel widths, which more frequently produces fractional remainders that trigger this rounding discrepancy.


Proposed Fix

Changing floor to ceil for EXACTLY mode in the pre-Android 15 path:

// TextLayoutManager.kt (after fix)
YogaMeasureMode.EXACTLY -> ceil(width).toInt()

I believe this aligns the measureLines StaticLayout width with the actual rendered width. Please let me know if there's a better approach or if I'm missing something.


iOS Comparison

While investigating, I noticed that the iOS equivalent (RCTTextLayoutManager.mm) has always used ceil():

// RCTTextLayoutManager.mm line 391
size = (CGSize){
    ceil(size.width  * layoutContext.pointScaleFactor) / layoutContext.pointScaleFactor,
    ceil(size.height * layoutContext.pointScaleFactor) / layoutContext.pointScaleFactor
};

iOS also uses CGFloat throughout the layout pipeline without integer pixel conversion, so this class of rounding issue doesn't occur there. This change seemed to bring Android's behavior in line with iOS.

Changelog:

[ANDROID] [FIXED] - Fix onTextLayout reporting incorrect line splits for single-line text in flex layouts

Test Plan:

Setup: On Android, render a <Text> with onTextLayout next to a flexGrow: 1 sibling in a row, and log (or display) the lines payload.

<View style={{ flexDirection: 'row' }}>
  <View style={{ flexGrow: 1 }} />
  <Text onTextLayout={e => console.log(e.nativeEvent.lines)}>
    Welcome!
  </Text>
</View>

Steps:

  1. Run on an Android device or emulator (prefer a non-integer density device such as Pixel 4 at 2.75x or a Galaxy-class device at 3.5x, where Yoga often yields fractional pixel widths).
  2. Wait for the layout to settle and onTextLayout to fire.
  3. Inspect lines.length and each line.text (e.g. via Logcat).

Before fix: The string visually fits on one line, but onTextLayout reports two lines with text split like "Welcome" and "!".

After fix: lines matches what is rendered: one line whose text is the full "Welcome!".

Related issues for context:

…ndroid

`measureLines()` passes `YogaMeasureMode.EXACTLY` to `createLayout()`.
In the pre-Android 15 path, `EXACTLY` mode used `floor(width)` to
compute the StaticLayout width, which could produce a layout that is
1px narrower than the width allocated by Yoga.

This causes single-line text (e.g. "Welcome!") to be reported as
multiple lines in `onTextLayout` when placed next to a `flexGrow: 1`
sibling — even though the text visually renders on one line.

Changing `floor` to `ceil` ensures the StaticLayout has at least
enough space to match what the TextView actually renders.

Fixes: facebook#54552
@meta-cla
Copy link
Copy Markdown

meta-cla bot commented Apr 4, 2026

Hi @dexical30!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla
Copy link
Copy Markdown

meta-cla bot commented Apr 4, 2026

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 4, 2026
@facebook-github-tools facebook-github-tools bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Apr 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant