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