Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text wrapping of several Text elements in react-native?

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:

enter image description here

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.jsonly. As a last resort, if absolutely necessary, I will restructure the contents of FormatText.js.

like image 481
John Avatar asked Mar 28 '18 13:03

John


People also ask

How do you wrap text in TextInput React Native?

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 {...

How do you stop text overflow in React Native?

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...


1 Answers

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>
like image 59
Pritish Vaidya Avatar answered Oct 17 '22 11:10

Pritish Vaidya