Skip to content

Commit

Permalink
Add a workaround for text being measured incorrectly when ending with…
Browse files Browse the repository at this point in the history
… an empty line (facebook#42331)

Summary:
It looks like `StaticLayout` used for measuring multiline text on the new architecture returns wrong metrics for the last line if it's empty, however, if it contains anything, then the returned values are correct.

Similar approach with measuring `I` was already used in `ReactEditText`: https://github.com/facebook/react-native/blob/258d8e51b451b221e557dad4647cbd210fe37392/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java#L1203-L1213

## Changelog:

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

[ANDROID] [FIXED] - Fixed text being measured incorrectly when ending with an empty line on the new architecture

Pull Request resolved: facebook#42331

Test Plan:
<details>
<summary>I tested the change on this simple code on React Native 0.73.2 (with the new arch enabled)</summary>

```jsx
import React from 'react';
import {useState} from 'react';
import {SafeAreaView, TextInput} from 'react-native';

export default function App() {
  const [text, setText] = useState('ABC\nDEF\nGHI\n');

  return (
    <SafeAreaView style={{flex: 1}}>
      <TextInput
        value={text}
        onChangeText={setText}
        style={{borderWidth: 1, margin: 20, padding: 30}}
        multiline
      />
    </SafeAreaView>
  );
}
```

</details>

Before:

https://github.com/facebook/react-native/assets/21055725/0787cc86-70c6-4238-b93a-87984e527a83

After:

https://github.com/facebook/react-native/assets/21055725/2acf619f-5cb4-4869-9576-cb8cc081c15b

Reviewed By: cipolleschi

Differential Revision: D62873674

Pulled By: cortinico

fbshipit-source-id: cc2481b1924a13af7326cfc16c59b9390f0d06d8
  • Loading branch information
j-piasecki authored and facebook-github-bot committed Sep 21, 2024
1 parent 1747f57 commit bd32392
Showing 1 changed file with 9 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,15 @@ private static Layout createLayout(
int hyphenationFrequency,
Layout.Alignment alignment) {
Layout layout;
// StaticLayout returns wrong metrics for the last line if it's empty, add something to the
// last line so it's measured correctly
if (text.toString().endsWith("\n")) {
SpannableStringBuilder sb = new SpannableStringBuilder(text);
sb.append("I");

text = sb;
}

int spanLength = text.length();
boolean unconstrainedWidth = widthYogaMeasureMode == YogaMeasureMode.UNDEFINED || width < 0;
float desiredWidth =
Expand Down

0 comments on commit bd32392

Please sign in to comment.