Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to touch and hold to select text in React Native

I'm new to Android and trying to implement a rich editor using Text component of React Native. However when I touch the text and hold for a moment, the text was not highlighted and the top bar with selection actions was not shown like what behaves in a webview or other applications. What am I missing or do I need to implement this feature by myself? If so could you point a direction?

like image 302
Sin Avatar asked Nov 16 '15 04:11

Sin


People also ask

How do you handle touch in React Native?

TouchableOpacity can be used to provide feedback by reducing the opacity of the button, allowing the background to be seen through while the user is pressing down. If you need to handle a tap gesture but you don't want any feedback to be displayed, use TouchableWithoutFeedback.

How do I make text copyable in React Native?

In react native we can make the Text component text Copyable using selectable={true} prop. This prop allows the Text to selectable by user.

How do you make a view touchable in React Native?

You will have to wrap your View inside TouchableHighlight or Opacity etc. I figured out a different way to solve my problem using pointerEvents={'box-none'} on my container View and wrapped the image in a View and gave it pointerEvents={'none'}.


3 Answers

If you want click-and-hold-to-select-text using a Text component, this feature does not currently exist in React Native for iOS. You probably want to upvote https://react-native.canny.io/feature-requests/p/support-selecting-text-within-text-elements-for-copypaste-etc to suggest Facebook add support for this.

However, I've submitted support for on Android, as of React 0.30. But React iOS is drawing characters directly to the screen (and not using the native UITextView that supports a selectable property), so it is not supported there, and likely won't be for a long time.

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.

like image 57
Mike Lambert Avatar answered Oct 12 '22 09:10

Mike Lambert


As of now it is supported by react-native as well by adding props selectable={true}. Also on Android you can change the selected text color with selectionColor='orange'.

Example:

<Text selectable={true} selectionColor='orange'>You can select me</Text>
like image 44
Muhammad Avatar answered Oct 12 '22 09:10

Muhammad


Be careful if the text is nested you should add selectable attribute to the top `

<Text selectable>
    <Text>
        some selectable text
    </Text>
</Text>
like image 9
Laurent Avatar answered Oct 12 '22 10:10

Laurent