score:1

    <View style={styles.imageCancel}>
         <TouchableOpacity onPress={() => { this.setModalVisible(!this.state.visible) }}>
                 <Text style={styles.textCancel} >Close</Text>
          </TouchableOpacity>
    </View>
const styles = StyleSheet.create({
 imageCancel: {
         height: 'auto',
         width: 'auto',
         justifyContent:'center',
         backgroundColor: '#000000',
         alignItems: 'flex-end',
    },
     textCancel: {
        paddingTop:25,
        paddingRight:25,
        fontSize : 18,
        color : "#ffffff",
        height: 50,
        },
 }};

enter image description here

score:1

Just add alignSelf to the text style, it won't take the whole width

 <View style={{flex: 1}}>
   <Text style={{alignSelf: 'center'}}>{'Your text here'}</Text>       
   <Text style={{alignSelf: 'baseline'}}>{'Your text here'}</Text>       
   <Text style={{alignSelf: 'flex-end'}}>{'Your text here'}</Text>
   <Text style={{alignSelf: 'flex-start'}}>{'Your text here'}</Text>
 </View>

These worked for me

score:4

It works with Nicholas answer unless you want to center the view somewhere. In that case, you could add a wrapper view around the view to obtain the width of its content and set alignItems: 'center'. Like this:

    <View style={{ flex: 1, alignItems: 'center' }}>
        <View style={{ justifyContent: 'center', alignItems: 'center', backgroundColor: 'red' }}>
            <Text>{'Your text is here'}</Text>
        </View>
    </View>

The background color is added to show the size of the inner view

score:8

Try this way for your requirement:

Here my height is Constant and i am enlarging the width by the length of text.

  <View style={{flexDirection: 'row'}}>
    <View style={{
        height: 30, width: 'auto', justifyContent:'center', 
        alignItems: 'center'}}>

        <Text style={{color:'white'}}>Now View will enlarge byitself</Text>

    </View>
  </View>

For Both Height and Width Try Changing the height inside the View style to 'auto' full code bellow:

  <View style={{flexDirection: 'row'}}>
    <View style={{
        height: 'auto', width: 'auto', justifyContent:'center', 
        alignItems: 'center'}}>

        <Text style={{color:'white'}}>Now View will enlarge byitself</Text>

    </View>
  </View>

Also try Giving padding to the View for arrange the text properly.

score:11

Or simply:

  <Text style={{color: '#ffffff', alignSelf: 'center'}}>Sample text here</Text>

score:13

This work for me.

<View style={{alignSelf: 'flex-start', backgroundColor: 'red'}}>
    <Text>abc</Text>
</View>

score:21

If you want to apply width to View based on <Text> , you can do something like this :

<View style={styles.container}>
   <Text>
     {text}
   </Text>
</View>

const styles = StyleSheet.create({
  container: {
    flexDirection:"row",
    alignSelf:"flex-start",
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
    padding: 8,
  }
});

WORKING DEMO

score:23

tldr: set alignItems to any value other than 'stretch' for the style of the parent View of your View containing Text

The issue your facing is likely related to React Native's default value for alignItems: 'stretch' on a <View /> element. Basically, all <View /> elements by default cause their children to stretch along the cross axis (axis opposite to the flexDirection). Setting alignItems to any value other than "stretch" ("baseline", "flex-start", "flex-end", or "center") on the parent View prevents this behavior and addresses your problem.

Below is an example where there are two View elements contained inside of a parent View with a blue border. The two View elements each contain a View wrapped around a Text element. In the case of the first View with default styling, the yellow child View expands horizontally to fill the entire width. In the second View where alignItems: 'baseline', the pink View does not expand and stays the size of it's child Text element.

enter image description here

<View style={{ borderWidth: 5, borderColor: 'blue' }}>
    <View>
        <View style={{ backgroundColor: 'yellow' }}>
            <Text style={{ fontSize: 30 }}>Hello</Text>
        </View>
    </View>
    <View style={{ alignItems: 'baseline' }}>
        <View style={{ backgroundColor: 'pink' }}>
            <Text style={{ fontSize: 30 }}>Hello</Text>
        </View>
    </View>
</View>

The align-items property is explained well here.

score:57

Two Solutions

Text component fits the text content

https://facebook.github.io/react-native/docs/text.html#containers

You can wrap <Text> components into another <Text> component.

By doing this everything inside is no longer using the flexbox layout but using text layout.

<Text>
   <Text>{'Your text here'}</Text>
</Text>

Render 1

View container adapt to nested Text component's content

In that case you need to use the props alignSelf in order to see the container shrink to the size of its content.

<View>
  <View style={{ alignSelf: 'center', padding: 12}} >
     <Text>{'Your text here'}</Text>
  </View>
</View>

Render 2

score:357

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

Related Query

More Query from same tag