反应原生 |向文本上下文菜单添加选项 [英] React Native | add option to Text context menu

查看:27
本文介绍了反应原生 |向文本上下文菜单添加选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个版本为 0.51 的 react-native 应用程序.在一个视图中,我想为文本选择上下文菜单添加一个新选项.

I am working on a react-native app with version 0.51. in one view I want to add a new option to text selection context-menu.

我没有在 react-native 的 Text 组件中找到任何属性来执行此操作.

I didn't find any property in the Text component of react-native to do this.

经过数小时的谷歌搜索,我通过在 AndroidManifest.xml

after many hours of googling I found this solution for android by adding the following in AndroidManifest.xml

<intent-filter>
    <action android:name="android.intent.action.PROCESS_TEXT" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/plain" />
</intent-filter>

这添加了一个名为Book App"的新选项

this added a new option with name of my application "Book App"

但我不认为这是最好的解决方案,因为:

but I don't feel it the best solution because :

1- 我需要在 android 平台代码中使用 react 来实现,以便在 android 和 iOS 上的行为相同
2- 我不知道如何更改选项的名称.
3- 我不知道单击此选项时如何触发特定操作.

1- I need to do it with react not in the android platform code to behave the same on android and iOS
2- I don't know how to change the name of the option.
3- I don't know how to trigger specific action when click this option.

在 Text 组件的上下文菜单中添加新选项的任何其他解决方案?

any other solution for adding new option in the context-menu of the Text component?

推荐答案

当我们在我的公司开发这个应用程序时,我们遇到了同样的问题,唯一的解决方案是在本地实现它,我们刚刚将其开源https://github.com/Astrocoders/react-native-selectable-text

We had the same problem when this app we were developing at my company and the only solution was to implement it natively, we've just open-sourced it https://github.com/Astrocoders/react-native-selectable-text

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

// Use normally, it is a drop-in replacement for react-native/Text
<SelectableText
  menuItems={['Foo', 'Bar']}
  /* Called when the user taps in a item of the selection menu, eventType is the label and content the selected text portion */
  onSelection={({ eventType, content }) => {}}
  /* iOS only (RGB) */
  highlightColor={[255, 0, 0]}
>
  I crave star damage
</SelectableText>

这篇关于反应原生 |向文本上下文菜单添加选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆