React Native文本溢出屏幕,拒绝自动换行。该怎么办?

312
以下代码可以在这个实例中找到。
我有以下的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

谢谢!

25个回答

0

我来到这里是因为我在寻找一个解决长单词换行/断行的方法。我找到了一个解决方案,那就是在<Text的'style'属性中使用'wordBreak'属性。使用wordBreak:break-word解决了我的问题。


0
文本溢出代码:
<View className="flex-row rounded-xl items-center p-2 pl-3 bg-red-400/20 space-x-2">
  <OrangeCircleIcon className="w-10 h-10" />
  <CText className="font-app-bold opacity-80">
    This is a Free Trial so you will not get charged anything now
  </CText>
</View>

文本不溢出容器代码:

<View className="flex-row rounded-xl items-center p-2 pl-3 bg-red-400/20 space-x-2">
  <OrangeCircleIcon className="w-10 h-10" />
  <View className="flex-1"> {/*  Notice this addition */}
    <CText className="font-app-bold opacity-80">
      This is a Free Trial so you will not get charged anything now
    </CText>
  </View>
</View>


-1

我的解决方案如下:

<View style={style.aboutContent}>
     <Text style={[styles.text,{textAlign:'justify'}]}>
        // text here                            
     </Text>
</View>

样式:

aboutContent:{
    flex:8,
    width:widthDevice-40,
    alignItems:'center'
},
text:{
    fontSize:widthDevice*0.04,
    color:'#fff',
    fontFamily:'SairaSemiCondensed-Medium'
},

结果: [d]我的结果[1]


所以你正在父元素上使用静态宽度,这正是我们想要避免的。 - SudoPlz

-2
<Text style={{width: 200}} numberOfLines={1} ellipsizeMode="tail">Your text here</Text>

请描述您回答中解决了什么问题,以及为什么解决了这个问题。 - Sebastian B.

-3
尝试在文本组件中使用此属性:adjustsFontSizeToFit={true}
<Text adjustsFontSizeToFit={true}>

这会使文本大小变小,但它不会换行文本,这正是问题的意图。 - SudoPlz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接