How can I show Read more button in Text if text length exceeds more than 2 lines? On Click of that Read more button a custom popup will be shown where full text will be visible. I have searched and checked here, here, here, but didn't found any proper solution to show Read more button as per requirement.
I tried this solution attached.. But here if I fixes numberOfLines to a value then "e.nativeEvent.lines.length" returns fixed value and if don't fixes numberOfLines then it returns complete text with ReadMore button which is wrong.
const ReadMore = props => {
const [showMore, setShowMore] = useState(false)
const longText = 'Nature, in the broadest sense, is the natural, physical, or material world or universe. "Nature" can refer to the phenomena of the physical world, and also to life in general. The study of nature is a large, if not the only, part of science. Although humans are part of nature, human activity is often understood as a separate category from other natural phenomena.'
return (
<View style={{
margin: 20,
}}>
<Text
numberOfLines={2}
onTextLayout={(e) => setShowMore(e.nativeEvent.lines.length > 2)}
>{longText}</Text>
{showMore && (
<Text style = {{color: 'blue'}} onPress = {() =>{
//Read more text action
}}>Read More...</Text>
)}
</View>
)
}