Let's say I have the following react-native code:
//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>;
And the styleOptions
is defined in this file.
//Style.js
const styleOptions = {
container: {
flexDirection: "row",
flexWrap: "wrap",
width: 300,
padding: 10
},
regular: { fontSize: 13 },
superscript: { fontSize: 8 },
strong: { fontSize: 13 }
};
I see the following output:
The problem is that there is a line break after "Super Pizza Store" and after the exponent. I'd imagine that's because each Text component is too long to fit in the space of 300px.
How do I get rid of line breaks and only allow for natural line breaks? Ideally, I want to limit my changes to Style.js
only. As a last resort, if absolutely necessary, I will restructure the contents of FormatText.js
.
To achieve the same effect, you can wrap your TextInput in a View : import React, { Component } from 'react'; import { AppRegistry, View, TextInput } from 'react-native'; class UselessTextInput extends Component { render() { return ( <TextInput {...
The only way to achieve this in React Native is to set position: absolute on the Text element on a flex-row container - quite the struggle, and definitely not the default...
Your parent element contains the styles
flexDirection:"row",flexWrap:"wrap",width:300,padding:10
therefore it will wrap the child elements if their width is less than 200
Since your child elements
are multiple
, therefore whoever doesn't satisfy the above condition gets wrapped as a whole.
For this you might consider using nested Text elements
as nested ones
<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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With