下面的代码可以在这个实际示例中找到

我有以下react原生元素:

'use strict';

var React = require('react-native');
var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
} = React;

var SampleApp = React.createClass({
  render: function() {
    return      (
  <View style={styles.container}>

        <View style={styles.descriptionContainerVer}>
          <View style={styles.descriptionContainerHor}>
            <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>
        </View>

        <View style={styles.descriptionContainerVer2}>
          <View style={styles.descriptionContainerHor}>
            <Text style={styles.descriptionText} numberOfLines={5} >Some other long text which you can still do nothing about.. Off the screen we go then.</Text>
          </View>
        </View>



  </View>);
  }
});
AppRegistry.registerComponent('SampleApp', () => SampleApp);

款式如下:

var styles = StyleSheet.create({
  container:{
        flex:1,
    flexDirection:'column',
        justifyContent: 'flex-start',
        backgroundColor: 'grey'
    },
    descriptionContainerVer:{
    flex:0.5, //height (according to its parent)
    flexDirection: 'column', //its children will be in a row
    alignItems: 'center',
    backgroundColor: 'blue',
    // alignSelf: 'center',
  },
  descriptionContainerVer2:{
    flex:0.5, //height (according to its parent)
    flexDirection: 'column', //its children will be in a row
    alignItems: 'center',
    backgroundColor: 'orange',
    // alignSelf: 'center',
  },
  descriptionContainerHor:{
    //width: 200, //I DON\'T want this line here, because I need to support many screen sizes
    flex: 0.3,  //width (according to its parent)
    flexDirection: 'column',    //its children will be in a column
    alignItems: 'center', //align items according to this parent (like setting self align on each item)
    justifyContent: 'center',
    flexWrap: 'wrap'
  },
  descriptionText: {
    backgroundColor: 'green',//Colors.transparentColor,
    fontSize: 16,
    color: 'white',
    textAlign: 'center',
    flexWrap: 'wrap'
  }
});

这将导致以下屏幕:

我如何阻止文本离开屏幕,并将其限制在屏幕中间,宽度为父屏幕的80%。

我不认为我应该使用宽度,因为我将在许多不同的手机屏幕上运行它,我希望它是动态的,所以我认为我应该完全依赖flexbox。

(这就是我在descriptionContainerHor中使用flex: 0.8的最初原因。

我想要实现的是这样的:

谢谢你!


当前回答

不幸的是,上述方法对我都不起作用。

我找到了这个npm包,你可以直接使用这个包: https://github.com/Bang9/react-native-wrapped-text

或者创建类似这样的东西:

<View style={{ alignItems: "center", alignSelf: "center", width: "100%" }}>
  <View style={{ flexDirection: "row", flexWrap: "wrap"}}>
    <Text style={{ textAlign: "center"}}>Long text, sooo looong...</Text>
  </View>
</View>

基于: https://github.com/Bang9/react-native-wrapped-text/blob/master/src/index.js

其他回答

这个问题的解决方案是flexShrink:

<View
    style={{ flexDirection: 'row' }}
> 
   <Text style={{ flexShrink: 1 }}>
       Really really long text...
   </Text>
</View>

根据你的设置,你也可能还需要添加flexShrink: 1到<View>的父母以及,让这个工作,所以玩,你会使它。

Adam Pietrasiak在这篇文章中发现了解决方案。

<SafeAreaView style={{flex:1}}>
  <View style={{alignItems:'center'}}>
    <Text style={{ textAlign:'center' }}>
      This code will make your text centered even when there is a line-break
    </Text>
  </View>
</SafeAreaView>
 <Text style={{ flexShrink: 1 }}>
       Really really long text   //line 1
       Really really long text2   // line 2 
   </Text>

现在文本不会离开屏幕WRK为我//

<视图风格= {{flexDirection:‘行’}}> <Text style={{flex: number}}>你错过了fdddddd dddddddd 你错过了 < /文本> < /视图>

{flex: aNumber}就是你所需要的!

只需将“flex”设置为适合您的数字。然后文本自动换行。

我认为你可以这样做,为了更好的想法,我附上了一张带有代码的图片:

所以,我们可以这样做:

TermsOfService = {
  fontFamily: 'Verlag-Book';
  fontSize: 14px;
  textAlign: center;
},
HighlightedText = {
  font-family: 'Verlag-Book';
  font-size: 14px;
  text-align: center;
  color: ${COLORS.PRIMARY_ADMIRAL_BLUE};
},
Container: {
  width: 100%;
  alignSelf: center;
  alignItems: center;
  justifyContent: center;
  flexDirection: row;
  flexWrap: wrap;
}

并且是你的组件,像这样使用它:

 <View style={Container}>
   <Text style={TermsOfService}>By joining, you agree to the some thing </Text>
   <Text style={HighlightedText}>this is highlighted </Text>
   <Text style={TermsOfService}>and </Text>
   <Text style={HighlightedText}>and this as well</Text>
 </View>