假设我有以下React Native代码:
//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 }
};
我看到了下面的输出:
问题是在“Super Pizza Store”和指数后面有一个换行。我想这是因为每个文本组件都太长,无法适应300px的空间。
如何消除换行符并仅允许自然换行?理想情况下,我只想限制对Style.js
的更改。如果绝对必要,我将重构FormatText.js
的内容作为最后的手段。
Style.js
来达到我想要的结果? - John<Text>
块会呈现为矩形块,这就是为什么它会将下一个文本块挤到另一行的原因。我猜没有样式规则可以将后续的文本块视为自然换行的“字符串”吧? - John