React native text going off my screen, refusing to wrap. What to do?

后端 未结 11 1514
栀梦
栀梦 2020-11-30 18:35

The following code can be found in this live example

I\'ve got the following react native element:

\'use strict\';

var React = require(\'react-nativ         


        
相关标签:
11条回答
  • 2020-11-30 19:35
    <Text style={{width: 200}} numberOfLines={1} ellipsizeMode="tail">Your text here</Text>
    
    0 讨论(0)
  • 2020-11-30 19:36

    This is a known bug. flexWrap: 'wrap' didn't work for me but this solution seems to work for most people

    Code

    <View style={styles.container}>
        <Text>Some text</Text>
    </View>
    

    Styles

    export default StyleSheet.create({
        container: {
            width: 0,
            flexGrow: 1,
            flex: 1,
        }
    });
    
    0 讨论(0)
  • 2020-11-30 19:40

    In version 0.62.2 of React Native, i just put "flex-shrink: 1" in Container of my "Text", but remember the flex-direction:row in View of container. Thank you guys for the help.

    My code:

    export const Product = styled.View`
      background: #fff;
      padding: 15px 10px;
      border-radius: 5px;
      margin: 5px;
      flex-direction: row;
    `;
    
    export const ProductTitleContainer = styled.View`
      font-size: 16px;
      margin-left: 5px;
      flex-shrink: 1;
    `;
    
    export const ProductTitle = styled.Text`
      font-size: 16px;
      flex-wrap: wrap;
    `;
    `;
    
    0 讨论(0)
  • 2020-11-30 19:41

    It works if you remove flexDirection: row from descriptionContainerVer and descriptionContainerVer2 respectively.

    UPDATE (see comments)

    I made a few changes to achieve what I think you're after. First of all I removed the descriptionContainerHor component. Then I set the flexDirection of the vertical views to row and added alignItems: 'center' and justifyContent: 'center'. Since the vertical views are now in fact stacked along the horizontal axis I removed the Ver part from the name.

    So now you have a wrapper view that should vertically and horizontally align it's content and stack it along the x-axis. I then simply put two invisible View components on the left and right side of the Text component to do the padding.

    Like this:

    <View style={styles.descriptionContainer}>
      <View style={styles.padding}/>
        <Text style={styles.descriptionText} numberOfLines={5} >
          Here is a really long text that you can do nothing about, its gonna be long wether you like it or not, so be prepared for it to go off screen. Right? Right..!
        </Text>
      <View style={styles.padding}/>
    </View>
    

    And this:

    descriptionContainer:{
      flex:0.5, //height (according to its parent),
      flexDirection: 'row',
      backgroundColor: 'blue',
      alignItems: 'center',
      justifyContent: 'center',
      // alignSelf: 'center',
    },
    padding: {
      flex: 0.1
    },
    descriptionText: {
      backgroundColor: 'green',//Colors.transparentColor,
      fontSize: 16,
      flex: 0.8,
      color: 'white',
      textAlign: 'center',
      flexWrap: 'wrap'
    },
    

    Then you get what I believe you were after.

    FURTHER IMPROVEMENTS

    Now if you would like to stack multiple text areas within the blue and orange views you can do something like this:

    <View style={styles.descriptionContainer2}>
      <View style={styles.padding}/>
      <View style={styles.textWrap}>
        <Text style={styles.descriptionText} numberOfLines={5} >
          Some other long text which you can still do nothing about.. Off the screen we go then.
        </Text>
        <Text style={styles.descriptionText} numberOfLines={5} >
          Another column of text.
        </Text>
      </View>
      <View style={styles.padding}/>
    </View>
    

    Where textWrapis styled like this:

    textWrap: {
      flexDirection: 'column',
      flex: 0.8
    },
    

    Hope this helps!

    0 讨论(0)
  • 2020-11-30 19:42
    <View style={{flexDirection:'row'}}> 
       <Text style={{ flex: number }}> You miss fffffdffffd ffffdffffffffd 
         You miss fdd
       </Text>
    </View>
    

    { flex: aNumber } is all you need!

    Just set 'flex' to a number that suit for you. And then the text will wrap.

    0 讨论(0)
提交回复
热议问题