Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React native text like span

Tags:

react-native

I tried to follow the solution in Simulate display: inline in React Native but it's not work.

I would like to do the same thing just like in HTML

First line is short so seems like no problem, but second line content is too long and it's expected to fill all the space before go to next line.

But my output is look like... output

<View style={styles.contentView}>
    <Text style={styles.username}>{s_username}</Text>
    <Text style={styles.content}>{s_content}</Text>
</View>

contentView: {
    paddingLeft: 10,
    flex: 1,
    flexDirection:'row',
    flexWrap:'wrap'
},
username: {
    fontWeight: 'bold'
},
content: {

}, 
like image 502
Wayne Tang Avatar asked Oct 03 '17 06:10

Wayne Tang


People also ask

Can I use span in React Native?

To add text like an HTML span with React Native, we can nest Text components. to add 2 Text components beside each other in an outer Text component. Therefore, we see 'foobar' on the screen since they're displayed inline.

How do you wrap text in React Native?

To wrap React Native text on the screen, we can set flexWrap to 'wrap' .

How do you justify text in React Native?

React Native provide textAlign style to make to make text justify but it's works in iOS, and android 8.0 ( Oreo ) or above (API level >= 26). The value will fallback to left on lower Android versions.

How do I truncate text in React Native?

To truncate text component using number of lines with React Native, we can get it from the onTextLayout callback's parameter. to set numberOfLines to NUM_OF_LINES to restrict the number of lines to display. We set ellipsizeMode to 'tail' to add the ellipsis after the truncated text.

What's the difference between React Native's font-family and fontfamily?

In React Native, we are more strict about it: you must wrap all the text nodes inside of a <Text> component. You cannot have a text node directly under a <View>. You also lose the ability to set up a default font for an entire subtree. Meanwhile, fontFamily only accepts a single font name, which is different from font-family in CSS.

What is the basic structure of a React Native App?

React Native is like React, but it uses native components instead of web components as building blocks. So to understand the basic structure of a React Native app, you need to understand some of the basic React concepts, like JSX, components, state, and props.

Can you have a text node under a view in React Native?

In React Native, we are more strict about it: you must wrap all the text nodes inside of a <Text> component. You cannot have a text node directly under a <View>. You also lose the ability to set up a default font for an entire subtree.

What is JSX in react?

In React, this is reversed. JSX lets you write your markup language inside code. It looks like HTML on the web, except instead of web things like <div> or <span>, you use React components. In this case, <Text> is a Core Component that displays some text and View is like the <div> or <span>. So this code is defining HelloWorldApp, a new Component.


Video Answer


2 Answers

React Native supports nested Text components, and you must use this to get your desired result. For example, you should have your second text component nested within your second, like so:

<View style={styles.contentView}>     <Text>         <Text style={styles.username}               onPress={() => {/*SOME FUNCTION*/} >            {s_username}         </Text>         <Text style={styles.content}>{s_content}</Text>     </Text> </View> 
like image 122
Ryan Turnbull Avatar answered Sep 17 '22 16:09

Ryan Turnbull


you can do as nesting text as text inside will consider as span like html

<View style={styles.contentView}>
  <Text style={styles.content}><Text style={styles.username}>{s_username}</Text> {s_content}</Text>
</View>

contentView: {
  paddingLeft: 10,
  flex: 1,
},
username: {
  fontWeight: 'bold'
},
content: {

}, 
like image 27
Mohamed Khalil Avatar answered Sep 16 '22 16:09

Mohamed Khalil