反应原生的几个文本元素的文本包装?

问题描述 投票:1回答:1

假设我有以下反应原生代码:

//FormatText.js

 <View style={styleOptions.container}>
<Text style={styleOptions.regular}>
    Hello world I am going to eat some Pizza for the sake of eating pizza.{" "}
</Text>
<Text style={[{ fontWeight: "bold" }, styleOptions.strong]}>
    Super Pizza Store.{" "}
</Text>
<Text style={styleOptions.regular}>You will pay $10</Text>
<Text style={[{ textAlignVertical: "top" }, styleOptions.superscript]}>
    8
</Text>
<Text style={styleOptions.regular}>. I doubt you can afford it.</Text>
</View>;

并且styleOptions在此文件中定义。

//Style.js
 const styleOptions = {
container: {
    flexDirection: "row",
    flexWrap: "wrap",
    width: 300,
    padding: 10
},
regular: { fontSize: 13 },
superscript: { fontSize: 8 },
strong: { fontSize: 13 }
};

我看到以下输出:

enter image description here

问题是“Super Pizza Store”之后和指数之后有一个换行符。我想这是因为每个Text组件太长而无法容纳在300px的空间中。

如何摆脱换行符,只允许自然换行?理想情况下,我想将我的更改限制为Style.jsonly。作为最后的手段,如果绝对必要,我将重组FormatText.js的内容。

react-native textwrapping
1个回答
1
投票

您的父元素包含样式

flexDirection:"row",flexWrap:"wrap",width:300,padding:10

因此,如果子元素的宽度小于200,它将包装子元素

由于你的child elementsmultiple,因此不满足上述条件的人将整体包裹起来。

为此,您可以考虑使用嵌套的Text elements作为嵌套的

<View style={styleOptions.container}>
            <Text style={styleOptions.regular}>Hello world I am going to eat some Pizza for the sake of eating pizza.
            <Text style={[{fontWeight:"bold"},styleOptions.strong]}>Super Pizza Store. </Text>
            <Text style={styleOptions.regular}>You will pay $10</Text>
            <Text style={[{textAlignVertical:'top'},styleOptions.superscript]}>8</Text>
                <Text style={styleOptions.regular}>.  I doubt you can afford it.</Text></Text>
        </View>
© www.soinside.com 2019 - 2024. All rights reserved.