facebook / react-native

A framework for building native applications using React
https://reactnative.dev
MIT License
118.9k stars 24.3k forks source link

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

Open yasemincidem opened 7 years ago

yasemincidem commented 7 years ago

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

hramos commented 7 years ago

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:

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.

ArsenyYankovsky commented 6 years ago

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

ArsenyYankovsky commented 6 years ago

@hramos Could you please reopen that?

kasiselvamk commented 6 years ago

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

tikkichan4 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

@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 commented 6 years ago

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!

michchan commented 6 years ago

I think this feature is necessary.

fenglu09 commented 6 years ago

+1

fenglu09 commented 6 years ago

@yasemincidem Have you found a solution?

react-native-bot commented 6 years ago

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

BichCNTT commented 6 years ago

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

ethanleeX commented 6 years ago

@BichCNTT you can try this https://github.com/status-im/status-react/issues/3616

wcandillon commented 6 years ago

@react-native-bot this issue should be reopened

singhsoldier13 commented 6 years ago

+1

thinhtrh commented 6 years ago

+1

yusent commented 6 years ago

+1

ezrra commented 6 years ago

+1

ghost commented 6 years ago

+1

axelfrid commented 6 years ago

+1

j-ornelas commented 5 years ago

+1

xinfang commented 5 years ago

+1

mraak commented 5 years ago

+1

AbdelhalimAhmed commented 5 years ago

+1

jeongsd commented 5 years ago

+1

kala888 commented 5 years ago

this issue should be reopened again

kala888 commented 5 years ago

Here is a temporary solution。

    {imageText.length > 0 &&
                device.ios && (
                  <AutoGrowingTextInput
                    scrollEnabled={false}
                    value={`        ${imageText}`}
                    editable={false}
                  />
                )}

              {imageText.length > 0 &&
                device.android && (
                  <Text selectable>
                    {'        '}
                    {imageText}
                  </Text>
                )}
msand commented 5 years ago

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 commented 5 years ago

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

kkusanagi commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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

acro5piano commented 4 years ago

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

scarlac commented 4 years ago

@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 commented 4 years ago

@scarlac Thank you for your answer!

fabOnReact commented 9 months ago

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 advantages would be:

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

johnlahut commented 8 months ago

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 commented 8 months ago

@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 commented 7 months ago

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 commented 7 months ago

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 commented 3 months ago

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.

samerce commented 3 months ago

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