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

我有以下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的最初原因。

我想要实现的是这样的:

谢谢你!


当前回答

尝试在这样的文本组件中使用这个prop adjustsFontSizeToFit={true}。

<Text adjustsFontSizeToFit={true}>

其他回答

你只需要有一个包装你的<Text>与flex如下;

<View style={{ flex: 1 }}>
  <Text>Your Text</Text>
</View>

这对我很有效

<View style={{flexShrink:1}}>
  <Text>some long random text...</Text>
</View>

这是一个已知的bug。flexWrap:“wrap”对我没用,但这个方法似乎对大多数人都有用

Code

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

风格

export default StyleSheet.create({
    container: {
        width: 0,
        flexGrow: 1,
        flex: 1,
    }
});
 <Text style={{ flexShrink: 1 }}>
       Really really long text   //line 1
       Really really long text2   // line 2 
   </Text>

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

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

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

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

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