简体   繁体   English

如何将嵌套文本包装在可触摸/可按下组件中?

[英]How Can I wrap a nested Text in a Touchable/Pressable component?

I am trying to wrap a nested text component in a TouchableOpacity or similar.我正在尝试将嵌套文本组件包装在 TouchableOpacity 或类似组件中。

The use case for this is to parse links and other special pieces of text out of a longer message and make them interactable (links, hashtags, etc...)这个用例是从较长的消息中解析链接和其他特殊文本片段,并使它们可交互(链接、主题标签等...)

ex.前任。 "Hello Testing [google.com] for [#energy] and [#candy] in the morning" “你好,早上测试 [#energy] 和 [#candy] [google.com]”

where [] represent interactable text其中 [] 代表可交互的文本

There are many questions like this on Stack Overflow, but in other cases, the OP was able to use a workaround that doesn't work in my case. Stack Overflow 上有很多这样的问题,但在其他情况下,OP 能够使用对我的情况不起作用的解决方法。

Here is an example:这是一个例子:

 <View style={someStyle}> <Text style={sharedTextStyles}> {textChunks.map((chunk:string)=>{ const text = ( <Text style={innerTextStyle}> {chunk} </Text> ) return matchesSomeRegex()? <TouchableOpacity onPressIn={someHandler0} onPress={someHandler1} onLongPress={someHandler2} > {text} </TouchableOpacity>: text })} </Text> </View>

The above code works, but the pressable chunks are seemingly impossible to position within the text lines (too high, and impossible to reposition with styling)上面的代码有效,但是文本行中的 position 似乎是不可能的(太高了,不可能通过样式重新定位)

I have tried splitting the string on " " and using flex positioning, which fixes the alignment, but creates new issues with text wrapping, spaces, and especially new lines that I can't think of a way to fix without writing a super complicated onLayout handling.我试过在“”上拆分字符串并使用 flex 定位,它修复了 alignment,但在文本换行、空格,尤其是新行方面产生了新问题,如果不编写一个超级复杂的 onLayout,我无法想出解决方法处理。

I am aware that the Text component has press handling, but between the horrible delayed visual feedback and the fact that it doesn't support onLongPress or onPressIn, this route is unworkable in my case.我知道 Text 组件具有按下处理功能,但在可怕的延迟视觉反馈和它不支持 onLongPress 或 onPressIn 的事实之间,这条路线在我的情况下是行不通的。

Does anyone know how to fix this issue?有谁知道如何解决这个问题?

I can recommend you a package that I recently used to separate url, hashtag and label in a social media application.我可以向您推荐一个 package,我最近在社交媒体应用程序中使用它来分隔 url、主题标签和 label。

https://www.npmjs.com/package/react-native-parsed-text https://www.npmjs.com/package/react-native-parsed-text

With this package, you can customize only many of your items and change the click events.使用此 package,您可以仅自定义许多项目并更改点击事件。

const hashtagPattern = /(#(?:[\w\ığüşöçİĞÜŞÖÇ]+))/g;
const hashtagRegex = new RegExp(hashtagPattern);

const labelPattern = /(@(?:[\w\ığüşöçİĞÜŞÖÇ]+))/g;
const labelRegex = new RegExp(labelPattern);

const urlPattern =
  /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/g;
const urlRegex = new RegExp(urlPattern);

<ParsedText
    style={{ color: '#000' }}
    parse={[
            {
                pattern: hashtagPattern,
                style: {color: 'red'},
                onPress: () => {},
                onLongPress: () => {},
                // Custom render
                // renderText: () => {}
            },
            {
                pattern: labelPattern,
                style: {color: 'blue'},
                onPress: () => {}
            },
            {
                pattern: urlPattern,
                style: {color: 'green'},
                onPress: () => {}
            },
        ]}>
        Hello World #hello
</ParsedText>

This is by no means a perfect solution, but I found a workaround.这绝不是一个完美的解决方案,但我找到了一个解决方法。 If you use translate to force the Touchable wrapped text back into place (by some constant factor of the line height) it produces good, consistent results.如果您使用 translate 强制 Touchable 换行文本回到原位(通过行高的某个常数因子),它会产生良好、一致的结果。 If anyone finds a better real solution, I will update.如果有人找到更好的实际解决方案,我会更新。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不能将 flexbox 用于可触摸的不透明度/可按压。 如何连续对齐这两个元素? - Can't I use flexbox for touchable opacity/ pressable. How to align these 2 elements in a row? 如何从另一个可触摸/可按下的按钮中按下按钮? [反应原生] - How can I press a button from within another touchable/pressable? [React Native] 如何在 React Native 的嵌套 Touchable 中传播触摸事件? - How can I propagate touch event in nested Touchable in React Native? 如何在Touchable组件中包含ScrollView? - How can I include a ScrollView inside a Touchable component? 当我在 reactnative 中使用 map 函数创建组件时,如何在 Pressable 中将颜色作为默认值? - How can I give a color as a defalut in Pressable when I create a component using the map function in reactnative? Touchable Opacity 嵌套在 View 组件内时不起作用,但如果将 Touchable opacity 设置为父组件以包裹其他组件,则可以使用 - Touchable Opacity not working when nested inside View component but works if Touchable opacity is made the parent component to wrap other components 在父容器之外时如何保持可触摸元素可按下 - How to keep touchable elements pressable when outside parent container 在可触摸的React Native UI组件包装中 - React Native UI Component Wrap in Touchable 在按下 Pressable 按钮组件时,我将按钮的 backgroundColor 更改为不同的颜色。 如何添加某种动画? - While a Pressable button component is pressed, I change the backgroundColor of a buton to a different color. How can I add some sort of animation? 在 Text 组件内垂直对齐 Pressable - Vertically align Pressable inside a Text component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM