Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

enter image description here

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 Lines I put under Testing Task, the more it gets 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.

like image 516
bryan Avatar asked May 24 '18 14:05

bryan


2 Answers

<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

like image 126
Roy Wang Avatar answered Oct 18 '22 19:10

Roy Wang


You can just add the line-height for the text, here you have the fontSize of 23, then you should be using the line-height of 27 or 28, basically the line height should be 4-5 pixel more than that of your text, that works fine for me

like image 2
Hardik poudel Avatar answered Oct 18 '22 19:10

Hardik poudel