React-native view auto width by text inside

xercool picture xercool · Jul 6, 2016 · Viewed 136.2k times · Source

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"

Answer

Nicholas Marcaccini Augusto picture Nicholas Marcaccini Augusto · Jul 26, 2017

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