javascript - Truncate Text - React Native - Stack Overflow

I have a React Native app with a FlatList.My logic that I have added was whenever the Character at 100t

I have a React Native app with a FlatList.

My logic that I have added was whenever the Character at 100th position is not empty an Expand/Collapse arrow should be added as shown below. NO arrow icon for short messages.

Well, this is bad logic!! Now when I change my app font to Large/small this logic won't work. It doesn't work for Chinese characters too LOL. So it shouldn't be based on number of characters.

{  alert.charAt(100) !== "" ?
                arrowClicked === true ? 
                <Icon type='materialicons' name='arrow-drop-up' onPress={()=>{this.setFullLength()}}  />
                : 
                <Icon type='materialicons' name='arrow-drop-down' onPress={()=>{this.setFullLength()}}  />
                : null
            } 

It should detect that the text is long and being truncated. How can I implement this?? Please Help!!!!

I have a React Native app with a FlatList.

My logic that I have added was whenever the Character at 100th position is not empty an Expand/Collapse arrow should be added as shown below. NO arrow icon for short messages.

Well, this is bad logic!! Now when I change my app font to Large/small this logic won't work. It doesn't work for Chinese characters too LOL. So it shouldn't be based on number of characters.

{  alert.charAt(100) !== "" ?
                arrowClicked === true ? 
                <Icon type='materialicons' name='arrow-drop-up' onPress={()=>{this.setFullLength()}}  />
                : 
                <Icon type='materialicons' name='arrow-drop-down' onPress={()=>{this.setFullLength()}}  />
                : null
            } 

It should detect that the text is long and being truncated. How can I implement this?? Please Help!!!!

Share Improve this question edited Jun 11, 2020 at 14:58 grooot asked Jun 11, 2020 at 3:11 groootgrooot 4662 gold badges12 silver badges33 bronze badges 2
  • 1 Maybe this can help you: css-tricks./line-clampin – julianobrasil Commented Jun 11, 2020 at 3:23
  • noo. For "font:normal" I can do this. Now I change font to "large" then this won't work right? – grooot Commented Jun 11, 2020 at 4:14
Add a ment  | 

1 Answer 1

Reset to default 6

You should use the onTextLayout and decide the line length using something like below.

function CustomText(props) {
  const [showMore, setShowMore] = React.useState(false);
  const [lines, setLines] = React.useState(props.numberOfLines);

  const onTextLayout = (e) => {
    setShowMore(
      e.nativeEvent.lines.length > props.numberOfLines && lines !== 0
    );
  };

  return (
    <View>
      <Text numberOfLines={lines} onTextLayout={onTextLayout}>
        {props.children}
      </Text>
      {showMore && (
        <Button title="Show More"
          onPress={() => {
            setLines(0);
            setShowMore(false);
          }}
        />
      )}
      {!showMore && (
        <Button title="Hide More"
          onPress={() => {
            setLines(props.numberOfLines);
          }}
        />
      )}
    </View>
  );
}

Usage

  const text =
    'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to mak';

 <CustomText numberOfLines={2}>{text}</CustomText>

You can pass other props like styles as well.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745367059a4624631.html

相关推荐

  • javascript - Truncate Text - React Native - Stack Overflow

    I have a React Native app with a FlatList.My logic that I have added was whenever the Character at 100t

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信