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

Click and hold to select text using a text component #13938

Open
yasemincidem opened this issue May 12, 2017 · 45 comments
Open

Click and hold to select text using a text component #13938

yasemincidem opened this issue May 12, 2017 · 45 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@yasemincidem
Copy link

yasemincidem commented May 12, 2017

Description

I want click-and-hold-to-select-text using a Text component and then i searched it and i found selectable property.It good works but it copy the entire text field.I want to selecting a subset of the text field for copying.Actually this question is asked on stackoverflow .Here is question on stackoverflow:http://stackoverflow.com/a/37119619/4596143

Reproduction Steps and Sample Code

I try this code block:

<Text
                        selectable={true}
                        style={styles.messageBody}>
                        {"text description"}
</Text>

This full of text ("text description") can copy not partially .I want to it like screenshot

Actual behaviour:
image

Expected behavior:

image

Solution

They are said that RN 0.39 adds support to copy the entire text field, but does not yet include support for selecting a subset of the text field for copying on stackoverflow .I wonder Text component include support for selecting a subset of the text field for copying on new versions for react native.If the property does not exist on new versions ,will the property for text integrate ?

Additional Information

  • React Native version:^0.43
  • Platform: IOS
  • Development Operating System: macOS
@hramos
Copy link
Contributor

hramos commented Jul 20, 2017

Hi there! This issue is being closed because it has been inactive for a while. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. Either way, we're automatically closing issues after a period of inactivity. Please do not take it personally!

If you think this issue should definitely remain open, please let us know. The following information is helpful when it comes to determining if the issue should be re-opened:

  • Does the issue still reproduce on the latest release candidate? Post a comment with the version you tested.
  • If so, is there any information missing from the bug report? Post a comment with all the information required by the issue template.
  • Is there a pull request that addresses this issue? Post a comment with the PR number so we can follow up.

If you would like to work on a patch to fix the issue, contributions are very welcome! Read through the contribution guide, and feel free to hop into #react-native if you need help planning your contribution.

@hramos hramos added the Icebox label Jul 20, 2017
@hramos hramos closed this as completed Jul 20, 2017
@ArsenyYankovsky
Copy link

This is still reproducible with "react-native": "0.49.0"

@ArsenyYankovsky
Copy link

@hramos Could you please reopen that?

@kasiselvamk
Copy link

Adding selectable={true} or selectable is not working.
v 0.51-RC

@tikkichan4
Copy link

tikkichan4 commented Jan 29, 2018

For me, both platforms are existing different problems :

In IOS, adding selectable={true} or selectable is not working for selecting partially text
In Android: adding selectable={true} or selectable is working but the nested image in text is disappeared for SOME devices
Environment: React Native v0.51

@pausabe
Copy link

pausabe commented Feb 5, 2018

I really need this feature... I'm on 0.52 v and I only can see "Copy" but not the expected behavior as it is said. Someone knows if there is a way to do so?

@tikkichan4
Copy link

tikkichan4 commented Feb 9, 2018

@pausabe If you really want this and you dont have complex nested component in Text component. Just using TextInput instead of Text and pass editable={false} as the prop and set the padding and margin as 0. The behavior is just same as Text but selectable. Hope this help.

@pausabe
Copy link

pausabe commented Feb 12, 2018

Thanks @tikkichan4! I've tried this in a simple text with the editable={false} but the text is not selectable...
Besides, I need this for complex nested Text so I image that this is not the way I should do it.
I hope Text component becomes more completed with this feature some day!

@hramos hramos removed the Icebox label Mar 8, 2018
@michchan
Copy link

I think this feature is necessary.

@hramos hramos added the Resolution: For Stack Overflow A question for Stack Overflow. Applying this label will cause issue to be closed. label Mar 20, 2018
@fenglu09
Copy link

+1

@fenglu09
Copy link

@yasemincidem Have you found a solution?

@react-native-bot
Copy link
Collaborator

This issue looks like a question and not an actual bug report. Please try asking over on Stack Overflow instead.

@BichCNTT
Copy link

BichCNTT commented May 9, 2018

Hello guys,
How I can get the word selected when set selectable={true}
Can you tell me?
Ths.

@ethanleeX
Copy link

@BichCNTT you can try this status-im/status-mobile#3616

@wcandillon
Copy link
Contributor

@react-native-bot this issue should be reopened

facebook-github-bot pushed a commit that referenced this issue Aug 9, 2018
Summary:
On iOS, it is not possible to select a range of text using a `Text` component (see #13938). Because of how the `Text` component is implemented on iOS, this will not work without a complete re-write. On Android however, this is not an issue.

As the `TextInput` component has evolved, it can more or less be used as a drop-in replacement on iOS by setting `multiline={true}` and `editable={false}`. Except for one detail: the text input field has scrolling activated and it's not possible to turn off. (See #1391 and #15962).

This pull request addresses that issue, simply by exposing the `scrollEnabled` property:

```
<TextInput
    multiline
    editable={false}
    scrollEnabled={false}
  />
```

1. Create a multiline `TextInput` component, with the attributes presented above.
2. Run on iOS
3. The `TextInput` field should not be able to scroll

facebook/react-native-website#367

[IOS] [FEATURE] [TextInput] - Made it possible to turn off scrolling on a multiline TextInput component
Pull Request resolved: #19330

Differential Revision: D9235061

Pulled By: hramos

fbshipit-source-id: 99d278004fc236b47dde7e61d74c71e8a3b9d170
kelset pushed a commit that referenced this issue Aug 13, 2018
Summary:
On iOS, it is not possible to select a range of text using a `Text` component (see #13938). Because of how the `Text` component is implemented on iOS, this will not work without a complete re-write. On Android however, this is not an issue.

As the `TextInput` component has evolved, it can more or less be used as a drop-in replacement on iOS by setting `multiline={true}` and `editable={false}`. Except for one detail: the text input field has scrolling activated and it's not possible to turn off. (See #1391 and #15962).

This pull request addresses that issue, simply by exposing the `scrollEnabled` property:

```
<TextInput
    multiline
    editable={false}
    scrollEnabled={false}
  />
```

1. Create a multiline `TextInput` component, with the attributes presented above.
2. Run on iOS
3. The `TextInput` field should not be able to scroll

facebook/react-native-website#367

[IOS] [FEATURE] [TextInput] - Made it possible to turn off scrolling on a multiline TextInput component
Pull Request resolved: #19330

Differential Revision: D9235061

Pulled By: hramos

fbshipit-source-id: 99d278004fc236b47dde7e61d74c71e8a3b9d170
@singhsoldier13
Copy link

+1

2 similar comments
@thinhtrh
Copy link

thinhtrh commented Sep 5, 2018

+1

@yusent
Copy link

yusent commented Sep 19, 2018

+1

@msand
Copy link
Contributor

msand commented Dec 12, 2018

I've published a workaround for now if you're interested: https://github.com/msand/react-native-selectable-text

It's only needed on Android, and works as a drop-in replacement for a TextInput with these specific values on iOS, as TextInput with editable: false already works correctly there. TextInput on Android either fills most of the screen with the keyboard, or disables the ability to select and respond to selection changes. I would assume this component only works well for the specific use-case I had, haven't tested it for any other prop values.

import SelectableText from 'react-native-selectable-text';

export default ({ text, onSelectionChange }) => (
  <SelectableText
    selectable
    multiline
    contextMenuHidden
    scrollEnabled={false}
    editable={false}
    onSelectionChange={onSelectionChange}
    style={{
      color: "#BAB6C8",
    }}
    value={text}
  />);

@zhouanbo
Copy link

My solution is to use https://www.npmjs.com/package/react-native-webview-autoheight and load text as html.

@kkusanagi
Copy link

kkusanagi commented May 28, 2019

React-Native 0.59 version. Only show COPY when long press.
Edit: Seems only work for Android version. IOS don't have this feature.

@scarlac
Copy link
Contributor

scarlac commented Aug 12, 2019

Use a <TextInput/> on iOS to allow multiple actions and use a regular <Text/> on Android. That will allow specific word selection on both platforms. No linking. No native libraries. Just use this:

import { Platform } from 'react-native';
// ...
{Platform.OS === 'ios' ? (
  // iOS requires a textinput for word selections
  <TextInput
    value={description}
    editable={false}
    multiline
  />
) : (
  // Android can do word selections just with <Text>
  <Text selectable>{description}</Text>
)}

Result, Android:
image

Result, iOS:
image

@hoanvnbka
Copy link

I test version 0.59.10 but selectable did not work. Does anyone have same error?

@acro5piano
Copy link

@scarlac
Thanks for the workaround. How do you treat nested <TextInput /> ? It seems to accept type of string.

@scarlac
Copy link
Contributor

scarlac commented Nov 26, 2019

@acro5piano TextInput cannot be nested in another TextInput. Un-nest your texts to make them selectable. If you need rich text formatting I unfortunately don't have an answer.

@hoanvnbka selectable is a react native feature that's usually reliable on iOS. On Android, the Emulator tends to stop responding to the long press after a while, so you'll need to restart the emulator to fix it. If you still need help, you need to provide more information than 'did not work'. Try posting on Stack Overflow with the solution I gave, a detailed explanation of the problem and what you tried to fix it.

@acro5piano
Copy link

@scarlac
Thank you for your answer!

@fabOnReact
Copy link
Contributor

Hello,

Thanks for the issue. I have been contributing to facebook/react-native for 4 years and specialize in the Text/TextInput components. I currently have 58 facebook/react-native PRs:

https://github.com/facebook/react-native/pulls?q=is%3Apr+author%3Afabriziobertoglio1987+

It is the suggested approach from the react-native core team (see this discussion).

I'm publishing several fixes for Text and TextInput component in a separate library react-native-improved.

  • The Component is based on ReactTextView, it is the same implementation from react-native.
  • It will include several bug fixes.
  • You can use the library until the PR is merged and released with react-native.
  • You can use side by side both components from react-native and react-native-improved, depending on where you need the fix.
  • I will also publish the PR to facebook/react-native (daily fixes and releases).

The advantages would be:

  • Increased engagement with the community, issues are quickly fixed.
  • No limitations when merging PRs (react-native main branch is used on facebook marketplace).
  • Only Meta employees can merge and review facebook/react-native PRs.
  • Allows us to further experiment and develop react-native.

Do you want me to publish for you a patched release? I'm working on a list of priorities. Thanks

@johnlahut
Copy link

Use a <TextInput/> on iOS to allow multiple actions and use a regular <Text/> on Android. That will allow specific word selection on both platforms. No linking. No native libraries. Just use this:

import { Platform } from 'react-native';
// ...
{Platform.OS === 'ios' ? (
  // iOS requires a textinput for word selections
  <TextInput
    value={description}
    editable={false}
    multiline
  />
) : (
  // Android can do word selections just with <Text>
  <Text selectable>{description}</Text>
)}

Result, Android: image

Result, iOS: image

@scarlac Are you aware of a way to make the <TextInput/> component selectable in iOS without having multiline set? In a few of my components I don't want text to wrap, but I'd still like the visible text to be selectable.

@scarlac
Copy link
Contributor

scarlac commented Feb 21, 2024

@johnlahut i'am not currently aware of a method but I haven't checked since that post so perhaps there are ways now that I'm not aware of. If you find one please share.

@AkbarBakhshi
Copy link

AkbarBakhshi commented Mar 12, 2024

Use a <TextInput/> on iOS to allow multiple actions and use a regular <Text/> on Android. That will allow specific word selection on both platforms. No linking. No native libraries. Just use this:

import { Platform } from 'react-native';
// ...
{Platform.OS === 'ios' ? (
  // iOS requires a textinput for word selections
  <TextInput
    value={description}
    editable={false}
    multiline
  />
) : (
  // Android can do word selections just with <Text>
  <Text selectable>{description}</Text>
)}

Result, Android: image

Result, iOS: image

This worked for me. Thanks 👍 . For me adding scrollEnabled={false} to the TextInput also helped with some undesired scrolling..

@haileyok
Copy link
Contributor

haileyok commented Mar 19, 2024

Dropping this here https://github.com/bluesky-social/react-native-uitextview

This works (mostly, it's a WIP but also not much time to fully implement everything) on iOS for text selection. Uses RN Text on Android and web since no changes are needed there, and a native component on iOS that uses UITextView over UILabel. We are using this in production though right now with no issues.

Contribs are more than welcome if you want to clean it up! Some styles are missing, etc. right now.

@fabOnReact Totally feel free to take it and clean it up if you ever want to. From what I've seen you do a lot of good work with Text so you probably have a way better idea of how to implement this than I do ❤️ (same to anyone else)

@lsarni
Copy link

lsarni commented Jul 11, 2024

In our case we need to have text formatting, and because the value of a TextInput can only be a string the workaround doesn't work. It would be great to have Text working on iOS the way it does on Android.

@bubblesunyum
Copy link

bubblesunyum commented Jul 29, 2024

surprised & devastated that this is not a react native feature :(
+1 to prioritize this since text is a fundamental piece of most apps and should be fully natively supported

@cortinico cortinico removed the Resolution: For Stack Overflow A question for Stack Overflow. Applying this label will cause issue to be closed. label Oct 15, 2024
@princektripathi
Copy link

princektripathi commented Nov 14, 2024

Hello @cipolleschi , @cortinico,

I debugged the partial text selection issue because it intrigued me, especially since it’s been open since 2017. Here are my findings:

  • RCTParagraphTextView extends UIView, which does not support partial text selection.
  • Partial text selection is only available in UIKit’s UITextView. Also, neither Text nor any View in SwiftUI supports partial selection.

Possible Solution

  • To support partial text selection, we would need to use UIKit’s UITextView.
  • Alternatively, we could implement custom partial text selection logic within UIView.

Cons

  • Using UITextView could limit the flexibility of custom drawing we initially intended by extending UIView.
  • Implementing custom partial selection logic adds unnecessary complexity, as it bypasses the native capabilities of UITextView

I created a sample app showcasing all major combinations of selectable texts:

  • UIKit: UITextView, UIView (manual text selection handling).
  • SwiftUI: Text, UIViewRepresentable (manual text selection handling).

Sample - https://github.com/PrinceK001/RNPlayground

Could you please validate my understanding?

API Ref -
Screenshot 2024-11-14 at 7 06 00 PM

@princektripathi
Copy link

@cortinico, @cipolleschi, there is another issue reported citing the exact same behaviour. Looking forward to hear your opinion and conclude this issue.

#33494

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests