Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-native view auto width by text inside

As far as I know, react-native stylesheet doesn't supports min-width/max-width property.

I have a view and text inside. The view in auto width doesn't resize by inherit text element.

How to fix that issue and make view width automatically set using text width?

My code is:

 <View style={{backgroundColor: '#000000'}}>       <Text style={{color: '#ffffff'}}>Sample text here</Text>  </View> 

In common HTML/CSS I would realize so:

 <div style="background-color: #000; display: inline;">Sample text here</div> 

Notice: flex: 1 on parent view is not helpful for me. Text appears as

"Sam" "ple" "Tex" "t" 
like image 413
xercool Avatar asked Jul 06 '16 21:07

xercool


People also ask

How do I get the screen width in React Native?

You can get the application window's width and height using the following code: const windowWidth = Dimensions. get('window').

How do I change the width of text in react?

In regular CSS you can give an element the property max-width: max-content , which makes the element (at most) as wide as the text inside of it. There's no direct equivalent in React Native, but instead you can use flexbox to emulate this behavior to make a Text element only as wide as the text inside.

What is flexGrow in React Native?

flexGrow describes how any space within a container should be distributed among its children along the main axis. After laying out its children, a container will distribute any remaining space according to the flex grow values specified by its children.


1 Answers

"alignSelf" does the same as 'display: inline-block' would in common HTML/CSS and it works very well for me.

<View style={{backgroundColor: '#000000', alignSelf: 'flex-start' }}>  <Text style={{color: '#ffffff'}}>   Sample text here  </Text> </View> 
like image 64
Nicholas Marcaccini Augusto Avatar answered Sep 20 '22 19:09

Nicholas Marcaccini Augusto