Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native: Determine number of lines of Text component

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.

like image 465
coldbuffet Avatar asked Jul 15 '16 01:07

coldbuffet


People also ask

How do you know if text is overflowing react native?

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.


2 Answers

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>   ); } 
like image 89
Arno Avatar answered Oct 09 '22 17:10

Arno


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'     } }) 
like image 36
Jimbo Magusib Avatar answered Oct 09 '22 17:10

Jimbo Magusib