React native 隐藏溢出时反应本机onTextLayout
我正在尝试在我的React原生应用程序中创建一个动画“查看更多/放弃”按钮。我计划使用onLayout或onTextLayout方法来获取文本的高度,然后使用动画和overflow:hidden在单击按钮时显示文本。下面是代码的样子:React native 隐藏溢出时反应本机onTextLayout,react-native,React Native,我正在尝试在我的React原生应用程序中创建一个动画“查看更多/放弃”按钮。我计划使用onLayout或onTextLayout方法来获取文本的高度,然后使用动画和overflow:hidden在单击按钮时显示文本。下面是代码的样子: <Animated.View style={{overflow: "hidden", height: heightAnim }}> <Text onTextLayout={({ native
<Animated.View style={{overflow: "hidden", height: heightAnim }}>
<Text
onTextLayout={({ nativeEvent: { lines } }) => {
//We set the maxHeight to number of lines * the height of a line
setContentHeight(lines.length * 14);
}}
>
{description}
//Description can range between 0 to 15 lines
</Text>
</Animated.View>
<TouchableWithoutFeedback onPress={() => handleOpen()}>
<Text>
{open ? "Discard" : "See More"}
</Text>
</TouchableWithoutFeedback>
然而,onLayout和onTextLayout只提供屏幕上可见文本的大小。
举个例子,如果文本持续14行,而我最初显示的2行高度为14,则onLayout将返回高度为28,而onTextLayout行将仅返回前2行。这导致动画的最大高度也为28,因此隐藏的文本保持隐藏状态
如何访问文本所有高度/行的高度,包括隐藏部分
谢谢你的帮助
<View>
<ScrollView style={{height:0}}>
<Text onTextLayout={(e)=>{/* measure the text here */}}>{text}</Text>
</ScrollView>
<Text>{text}</Text>
</View>
您可以渲染文本两次,其中一次使用高度为0的滚动视图包装文本。然后可以测量文本的所有高度/行