react-native icon indicating copy to clipboard operation
react-native copied to clipboard

Add a workaround for text being measured incorrectly when ending with an empty line

Open j-piasecki opened this issue 1 year ago • 4 comments

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:

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

Test Plan:

I tested the change on this simple code on React Native 0.73.2 (with the new arch enabled)
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>
  );
}

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

j-piasecki avatar Jan 17 '24 12:01 j-piasecki

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 16,798,999 +7
android hermes armeabi-v7a n/a --
android hermes x86 n/a --
android hermes x86_64 n/a --
android jsc arm64-v8a 20,190,313 -5
android jsc armeabi-v7a n/a --
android jsc x86 n/a --
android jsc x86_64 n/a --

Base commit: 6c8dfc89566d27c36f9ca3828bcf3a63dfcb6942 Branch: main

analysis-bot avatar Jan 17 '24 13:01 analysis-bot

This PR is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Jul 16 '24 05:07 github-actions[bot]

Not stale

j-piasecki avatar Jul 18 '24 07:07 j-piasecki

@cortinico has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

facebook-github-bot avatar Sep 17 '24 14:09 facebook-github-bot

@cortinico merged this pull request in facebook/react-native@bd323929dc5be5666ee36043babec7d981a095dc.

facebook-github-bot avatar Sep 21 '24 14:09 facebook-github-bot

This pull request was successfully merged by @j-piasecki in bd323929dc5be5666ee36043babec7d981a095dc

When will my fix make it into a release? | How to file a pick request?

react-native-bot avatar Sep 21 '24 14:09 react-native-bot

FYI that https://github.com/facebook/react-native/pull/46613 replaces this logic with a proper fix (we were not correctly propagating settings to TextPaint used for layout)

NickGerleman avatar Sep 24 '24 06:09 NickGerleman