css - React 原生文本离开我的屏幕,拒绝换行。该怎么办?

标签 css reactjs react-native flexbox

以下代码可以在this live example中找到

我有以下 react native 元素:

'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'
  }
});

这会产生以下屏幕:

Text off screen app

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

我认为我不应该使用 width 因为我将在许多不同的移动屏幕上运行它并且我希望它是动态的,所以我认为我应该完全依赖 flexbox

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

我想要实现的是这样的:

What I want to achieve

谢谢!

最佳答案

我从下面的链接找到了解决方案。

[Text] Text doesn't wrap #1438

<View style={{flexDirection:'row'}}> 
   <Text style={{flex: 1, flexWrap: 'wrap'}}> You miss fdddddd dddddddd 
     You miss fdd
   </Text>
</View>

Output

如果你想感谢他,下面是 Github 个人资料用户链接。

Ally Rippley


编辑:2019 年 4 月 9 日星期二

正如@sudoPlz 在评论中提到的,它与 flexShrink: 1 一起更新此答案。

enter image description here

关于css - React 原生文本离开我的屏幕,拒绝换行。该怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36284453/

相关文章:

android - 仅学习 React Native 是否足以构建 iOS 和 Android 应用程序?

javascript - 尝试使用自定义图标创建 TabBarIOS 时发生不变冲突错误

ios - 如何在react-native中制作 map 的iOS底部面板?

css - 从原点悬停时触发 translateY()

html - 强制同一行中的多个图像按比例调整大小,而不是将它们移动到新行

javascript - 在 react 中从同一文件导入多个组件而不是导入每个组件的正确方法是什么

java - Selenium 中的页面加载超时

javascript - 当用户从 React Native 中的另一个页面导航回页面时,如何重新加载页面?

html - 水平居中页脚 div

javascript - 如何使 JQuery 插件 "Rounded Corners"工作? (或提出更好的选择)