React-Native text get's vertically cut off for no reason

强颜欢笑 提交于 2019-12-23 06:56:32

问题


I have an interesting bug that is happening to my text. For some reason text is randomly getting cut off like so:

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'flex-start',
    backgroundColor: "#ecf0f1",
    width:"100%",
    paddingTop:"5%"
  },
  itemContainer: {
    backgroundColor:"#fff",
    margin:"5%",
    marginTop: 0,
    borderRadius:5,
    width: "90%"
  },
  itemHeaderContainer: {
    padding:15,
    borderColor: "#E4E2E9",
    borderBottomWidth: 1
  },
  itemHeaderText: {
    height:'auto',
    color:"#333",
    fontSize: 23,
    fontWeight: "800",
  },
  itemButtonContainer: {padding:13, flexWrap: 'wrap',  alignItems: 'flex-start', flexDirection:'row'},
  itemButtonText: { fontSize:19, color:"#fff", fontWeight:"800" },
  itemCreateButton: { backgroundColor:"#3F61E7", borderRadius: 5, paddingVertical:10, paddingHorizontal:15},
});

renderTemplate() {
  if(this.state.loaded) {
    return (
      <View style={{width:"100%"}}>

      <View style={styles.itemContainer}>
        <View style={[styles.itemHeaderContainer, {borderBottomWidth: 0}]}>
          <Text style={styles.itemHeaderText}>{this.state.task_data.title}</Text>
          <Text style={{ marginTop:10, fontSize:18, color:"#333", fontWeight:"400" }}>Line 1</Text> 
          <Text style={{ marginTop:10, fontSize:18, color:"#333", fontWeight:"400" }}>Line 2</Text>
          <Text style={{ marginTop:10, fontSize:18, color:"#333", fontWeight:"400" }}>Line 3</Text> 
          <Text style={{ marginTop:10, fontSize:18, color:"#333", fontWeight:"400" }}>Line 4</Text> 
          <Text style={{ marginTop:10, fontSize:18, color:"#333", fontWeight:"400" }}>Line 5</Text> 
        </View>
      </View>

      <View style={[styles.itemContainer, {padding:15}]}>
        <Text style={[styles.itemHeaderText, {}]}>Cut off Text????</Text>
      </View>

      <View style={styles.itemContainer}>
        <View style={styles.itemHeaderContainer}>
          <Text style={styles.itemHeaderText}>Another Section</Text>
        </View>
        <View style={styles.itemButtonContainer}>
            <TouchableHighlight underlayColor='#3F61E7' style={[styles.itemCreateButton, {marginRight: 10}]}>
              <View style={{flexWrap: 'wrap',  alignItems: 'flex-start', flexDirection:'row'}}>
                <Text style={styles.itemButtonText}>Button 1</Text>
              </View>
            </TouchableHighlight>

             <TouchableHighlight underlayColor='#3F61E7' style={styles.itemCreateButton}>
              <View style={{flexWrap: 'wrap',  alignItems: 'flex-start', flexDirection:'row'}}>
                <Text style={styles.itemButtonText}>Button 2</Text>
              </View>
            </TouchableHighlight>
        </View>
      </View>

      <View style={styles.itemContainer}>
        <View style={styles.itemHeaderContainer}>
          <Text style={styles.itemHeaderText}>Existing Documents</Text>
        </View>

        <FlatList data={this.state.task_documents} style={{ paddingBottom:10, paddingHorizontal:0 }} renderItem={
          ({item}) => (

            <View style={{ borderBottomWidth:1, borderColor:"#F1F0F3"}}>
              <View style={[{flexGrow:1, paddingHorizontal:5, flex:1, }]}>
                <Text numberOfLines={1} ellipsizeMode='tail' style={{ flexShrink: 1, fontSize:24, fontWeight:"600",}}>{item.value.title || "No Title"}</Text>
              </View>
            </View>
          )
        } />
      </View>

      </View>
    );
  }
  else return (
    <View style={styles.itemContainer}>
      <View style={[styles.itemHeaderContainer, {borderBottomWidth: 0}]}>
        <Text style={styles.itemHeaderText}>Loading item..</Text>
      </View>
    </View>
  );
}

render() {
  return (
    <ScrollView>
      <View style={styles.container}>
        {this.renderTemplate()}          
      </View>
    </ScrollView>
  );
}

Interestingly enough, the more Line's I put under Testing Task, the more it get's cut off.

  • If I move everything from renderTemplate() to render(), it doesn't get cut off
  • If I remove the lines entirely, the text doesn't get cut off.
  • If I replace FlatList return to null or remove it, it doesn't get cut off.
  • Basically when I start doing random removals of content, things start affect other elements in weird ways.

Has this ever happened to anyone else? Am I doing something wrong? Open to any and all suggestions.


回答1:


<View style={[styles.itemContainer, { padding: 15 }]}>
  <Text style={styles.itemHeaderText}>Cut off Text????</Text>
</View>

The padding should be applied to the Text component instead of the View container:

<View style={styles.itemContainer}>
  <Text style={[styles.itemHeaderText, { padding: 15 }]}>Cut off Text????</Text>
</View>

Working code: https://snack.expo.io/Hkn9YIC17



来源:https://stackoverflow.com/questions/50511348/react-native-text-gets-vertically-cut-off-for-no-reason

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!