【问题标题】:React-native text layoutReact-native 文本布局
【发布时间】:2021-04-24 09:44:07
【问题描述】:

我有以下 react-native 组件:

const TaskItem = () => {
  return (
    <>
      <View
        style={{
          marginTop: 32,
          marginHorizontal: 8,
          display: 'flex',
          flexDirection: 'row',
          backgroundColor: 'red',
        }}
      >
        <Text style={{ marginRight: 8 }}>
          <Text style={{ backgroundColor: 'green' }}>Title:</Text>
        </Text>
        <Text>
          <Text
            style={{
              fontWeight: 'bold',
              color: '#000000',
              backgroundColor: 'blue',
            }}
            numberOfLines={2}
          >
            Very long text Very long text Very long text Very long text Very long text
          </Text>
        </Text>
      </View>
    </>
  );
};

它会产生以下屏幕(用于调试的背景颜色):

我想要的是:

  1. 蓝色文字不会溢出右侧的红色容器(尊重marginHorizontal: 8
  2. 蓝色文本从左侧红色容器的开头开始(换行)

【问题讨论】:

    标签: javascript css react-native react-native-paper


    【解决方案1】:

    这就是解决方案。从实现中检查this

    您的TaskItem 应该是这样的

    const TaskItem = () => {
      return (
        <View
          style={{
            marginTop: 32,
            marginHorizontal: 8,
            flexDirection: 'row',
            borderColor: 'black',
            borderWidth: 1,
          }}>
          <Text style={{ marginRight: 8 }}>Title:</Text>
          <View style={{ flex: 1, flexDirection: 'row' }}>
            <Text
              style={{
                fontWeight: 'bold',
                color: '#000000',
              }}
              numberOfLines={2}>
              Very long text Very long text VeVery long text Very long text Very
              long text Very long text VeryVery long text Very long text Very long
              text Very long text VeryVery long text Very long text Very long text
              Very long text VeryVery long text Very long text Very long text Very
              long text Veryry long text Very long text Very long text
            </Text>
          </View>
        </View>
      );
    };
    

    【讨论】:

      猜你喜欢
      • 2019-04-28
      • 2019-09-20
      • 1970-01-01
      • 2015-10-06
      • 2016-04-11
      • 2023-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多