As the title says, I've been trying to find a way to determine the number of lines the text component AFTER it has been given text. Look at my example below.
<Text> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi semper ut ipsum in ultrices. Vivamus fringilla lacinia odio in accumsan. Proin sit amet pellentesque tortor. Nam mollis sit amet ligula id convallis. Etiam in semper diam. Cras id elit consectetur, interdum ante id, tincidunt nisi. Integer non elit placerat, dignissim nibh at, faucibus sem. Curabitur nec posuere turpis. Vivamus rhoncus nulla vitae mi imperdiet, elementum eleifend mi laoreet. Vestibulum molestie turpis non nibh elementum, sed ornare magna tristique. Aliquam erat volutpat. Phasellus volutpat mi vel tempor finibus. </Text>
At runtime, how can I determine how many lines this Text component has rendered. This number will vary depending on device (eg. iPhone 5 will need to render more lines vs iPhone 6+ as it has a smaller screen size). I've checked the source code for the Text component but there doesn't seem to be anything I'm looking for.
I am using React Native 0.24.
Any ideas?
Cheers.
The width of a text container can be obtained similarly via onLayout or computed based on flex. Then we only need to compare the text width with the container width to determine whether there is overflow.
I want to provide a modern solution. There is now a onTextLayout
event that includes an array of lines
which can be determined what number of lines are being rendered. There's other details in the lines
array like actual height and width of every line which can be further used to determine if the text is being truncated.
const NUM_OF_LINES = 5; const SOME_LONG_TEXT_BLOCK = 'Lorem ipsum ...'; function SomeComponent () { const [ showMore, setShowMore ] = useState(false); const onTextLayout = useCallback(e => { setShowMore(e.nativeEvent.lines.length > NUM_OF_LINES); }, []); return ( <Text numberOfLines={NUM_OF_LINES} onTextLayout={onTextLayout}> {SOME_LONG_TEXT_BLOCK} </Text> ); }
This one is working for both IOS AND Android
const [ loadMore, setLoadMore ] = useState(false); const [ numOfLines, setNumOfLines ] = useState(0); const onTextLayout = useCallback(e => { if(numOfLines == 0) setNumOfLines(e.nativeEvent.lines.length); }); const onLoadMoreToggle = () => { setLoadMore(!loadMore); } return ( <View style={styles.container}> <Text numberOfLines={numOfLines == 0 ? null : loadMore ? numOfLines : NUM_OF_LINES} onTextLayout={onTextLayout} style={styles.bodyText} > {props.children} </Text> { (numOfLines > NUM_OF_LINES) && <View style={styles.linkContainer}> <TouchableRipple onPress={onLoadMoreToggle}> <Text style={styles.linkText}>{ loadMore? 'Load Less' :'Load More'}</Text> </TouchableRipple> </View> } </View> ) const styles = StyleSheet.create({ container: { display: 'flex', flexDirection:'column', }, bodyText: { flex:1, }, linkContainer: { flexDirection: 'row', justifyContent: 'flex-end' }, linkText: { color: '#2196f3' } })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With