Skip to content

Commit 7b6e790

Browse files
authored
fix: paint Android text decorations when a text update keeps the view's frame
This is #58579 by @MatiPl01, a fix for a bug I found. Mati is no longer at Software Mansion, so I'm picking this up from my fork. Thanks Mati! ## Summary: Since #56768 underline and strikethrough are painted by `ReactTextView.onDraw` through `CanvasEffectSpan`s, and only when `getLayout()` is non-null. `TextView.setText` drops the `Layout` and rebuilds it in `onMeasure`, but Fabric measures a view only when its frame changed. A text update that keeps the frame (`textDecorationLine` or `textDecorationColor` changing on a text that keeps its size) therefore reaches `onDraw` without a `Layout`, the decorations are skipped and `super.onDraw()` paints plain text; they show up only once something else re-measures the view. `onDraw` now rebuilds the `Layout` at the current size before painting the effects; `TextView.onDraw` would build one for the text anyway. ## Changelog: [ANDROID] [FIXED] - Draw text decorations after a text update that did not change the view's frame ## Test Plan: Android emulator API 34, RN 0.88.0-rc.1. With logging in `onDraw`, every frame-preserving update arrived as `setText -> onDraw` with `getLayout() == null` and no decoration painted, both for `setState` toggling `textDecorationLine` and for a Reanimated animation updating it every frame; the same update followed by a frame change painted it. `textDecorationLine` cycling `none` / `underline` / `line-through` / `underline line-through` every 0.75 s, before and after: https://github.com/user-attachments/assets/7aecf0a9-f999-4f1b-9699-fd07be53cb53 Differential Revision: D129144546 Pull Request resolved: #58847
1 parent 875656a commit 7b6e790

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

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

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,15 @@ protected void onDraw(Canvas canvas) {
289289

290290
if (spanned != null) {
291291
Layout layout = getLayout();
292+
if (layout == null && getWidth() > 0 && getHeight() > 0) {
293+
// setText drops the Layout and Fabric re-measures only on a frame change, so a text
294+
// update that kept the frame reaches onDraw without one and the effects below would be
295+
// skipped. Rebuild it here; TextView.onDraw would build one for the text anyway.
296+
measure(
297+
MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
298+
MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY));
299+
layout = getLayout();
300+
}
292301
if (layout != null) {
293302
CanvasEffectSpan[] drawSpans =
294303
spanned.getSpans(0, spanned.length(), CanvasEffectSpan.class);

0 commit comments

Comments
 (0)