我正在开发反应本机应用程序。在其中,我们展示了一些描述Text, 可能是行数。
因此,如果数据超过 3 行,如果扩展,我必须显示更多和更少。
<FlatList
style={styles.faltList}
showsVerticalScrollIndicator
data={data}
extraData={this.state}
renderItem={({ item, index }) => (
<View style={styles.flatListCell}>
<Text style={styles.description}>{item.description}</Text>
</View>
</View>
)
}
ItemSeparatorComponent={() => (
<View style={{ height: 10}} />
)}
/>
我已经发现反应本机查看更多文本库,但我想通过自定义代码来实现它。
注意:我正在 FlatList 中显示该文本。
有什么建议么?
我尝试过这种方式,希望对你和其他人有帮助!
const postTextContent = (props) => {
const [textShown, setTextShown] = useState(false); //To show ur remaining Text
const [lengthMore,setLengthMore] = useState(false); //to show the "Read more & Less Line"
const toggleNumberOfLines = () => { //To toggle the show text or hide it
setTextShown(!textShown);
}
const onTextLayout = useCallback(e =>{
setLengthMore(e.nativeEvent.lines.length >=4); //to check the text is more than 4 lines or not
// console.log(e.nativeEvent);
},[]);
return (
<View style={styles.mainContainer}>
<Text
onTextLayout={onTextLayout}
numberOfLines={textShown ? undefined : 4}
style={{ lineHeight: 21 }}>{Your Long Text}</Text>
{
lengthMore ? <Text
onPress={toggleNumberOfLines}
style={{ lineHeight: 21, marginTop: 10 }}>{textShown ? 'Read less...' : 'Read more...'}</Text>
:null
}
</View>
)
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)