Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Text on Android]: setting selectable to true breaks the text truncation and lineHeight on Android #48921

Open
dariakoko opened this issue Jan 24, 2025 · 13 comments
Labels

Comments

@dariakoko
Copy link

dariakoko commented Jan 24, 2025

Description

I use Text component that has to contain selectable text and should be truncated if it extends the width of the parent component.
On iOS everything works fine. On Android if I set selectable={true} truncation and line height of the text are breaking.

Steps to reproduce

  1. import Text component from react-native
  2. render Text component, add selectable={true}, numberOfLines={1}
  3. the text is cut off instead of being truncated, the line height is bigger than it should be, the top of the second line of text is visible

React Native Version

0.75.4

Affected Platforms

Runtime - Android

Output of npx react-native info

-

Stacktrace or Logs

-

Reproducer

https://github.com/dariakoko/text-component-on-android

Screenshots and Videos

Image
      <View style={{width: 200}}>
        <Text style={{color: 'green'}}>default: </Text>
        <Text style={{backgroundColor: 'yellow'}}>
          Some long very very very very very long text
        </Text>

        <Text>-------------</Text>

        <Text style={{color: 'green'}}>not selectable: </Text>
        <Text numberOfLines={1} style={{backgroundColor: 'yellow'}}>
          Some long very very very very very long text
        </Text>

        <Text>-------------</Text>

        <Text style={{color: 'red'}}>selectable (issue): </Text>
        <Text
          selectable={true}
          numberOfLines={1}
          style={{backgroundColor: 'yellow'}}>
          Some long very very very very very long text
        </Text>
        <View style={{height: 20, backgroundColor: 'pink'}}></View>
      </View>
@spyshower
Copy link

what happens if you render something extra below the text? in your example, that would be below the red text (the actual text, not the label)

@dariakoko
Copy link
Author

@spyshower like this?
Nothing changes

Image
        <Text style={{color: 'red'}}>selectable (issue): </Text>
        <Text selectable={true} numberOfLines={1}>
          Some long very very very very very long text
        </Text>
        <View style={{height: 20, backgroundColor: 'pink'}}></View>

@dariakoko
Copy link
Author

dariakoko commented Jan 24, 2025

Set the background color for the text, as you can see it's the actual text component that gets all weird

Image
        <Text
          selectable={true}
          numberOfLines={1}
          style={{backgroundColor: 'yellow'}}>
          Some long very very very very very long text
        </Text>

@dariakoko
Copy link
Author

Feels like the numberOfLines prop is not being handled correctly if selectable is set to true

@spyshower
Copy link

I am not able to check your code in android atm, any chance you played with ellipsizeMode? on the docs it says:

On Android, when numberOfLines is set to a value higher than 1, only tail value will work correctly.

as a nasty workout, I believe you will be able to get it to work if you specify a height to the parent controler.

i will try to reproduce it later

@dariakoko
Copy link
Author

Thanks!
It's too nasty tho 😄 and the missing truncation won't be solved by specify the height

@spyshower
Copy link

Thanks! It's too nasty tho 😄 and the missing truncation won't be solved by specify the height

True I guess...

@princektripathi
Copy link
Collaborator

@dariakoko, I validated the repro and it seems to be working fine. Am I missing something?

Screen.Recording.2025-01-27.at.7.51.17.PM.mov

@spyshower
Copy link

@dariakoko, I validated the repro and it seems to be working fine. Am I missing something?
Screen.Recording.2025-01-27.at.7.51.17.PM.mov

can you share the code too? :)

@yogeshpaliyal
Copy link

yogeshpaliyal commented Jan 28, 2025

I Guess, I found the Issue whenever we set the numberOfLines as 1, we have to set setSingleLine(true) on the native TextView (this is currently not happening), I tried by adding the setSingleLine and setting the value as true, it is working as expected.

  1. numberOfLines : 1
  2. selectable : true
  3. singleLine : true (set from android native hardcoded)
Image

But there is still a issue, if we set the numberOfLines to more than 1, then ellipsizeMode and selectable not working together.

  1. numberOfLines : 2
  2. selectable : true
  3. singleLine : false (set from android native hardcoded)
Image

@dariakoko
Copy link
Author

@princektripathi hi! I still see the issue on the screenshot you provided - the text inside of the third yellow component is not truncated and cut out instead, am expecting three dots in the end of the line.

@yogeshpaliyal thank you for looking into it too! I am not that familiar with the native code, but from what you described it looks like this would be the solution. The only minor issue I see from the screenshot you provided is the extra space before the three dots

@yogeshpaliyal
Copy link

@dariakoko is the spacing issue is in 1st screenshot or 2nd one?

@dariakoko
Copy link
Author

@yogeshpaliyal first

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants