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,
},
}};
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,
}
});
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.
<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>
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>
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>
Source: stackoverflow.com
Related Query
- React-native view auto width by text inside
- react native Scroll View doesn't scroll from inside text input
- React Native Android error trying to nest a view inside a text
- Make view 80% width of parent in React Native
- Scroll View inside view not working react native
- Wrap children inside view React native
- React Stripe Elements - set width to auto for iFrame? Elements invisible inside grid and modal
- React native text going off my screen, refusing to wrap. What to do?
- How to set image width to be 100% and height to be auto in react native?
- Checking text appears inside an element using react testing library
- How to adjust font size to fit view in React Native for Android?
- React native text component using number of lines
- React Native multi line TextInput, text is centered
- React Select auto size width
- Forcing onLayout on React Native view
- Show only the 1st line of text in React Native
- Text is getting cut off in android for react native
- Center a text in the screen with react native
- React native (expo) web view Error net::ERR_CACHE_MISS
- Unload / Release / Remove React Native View from runtime
- Detect tap on the outside of the View in react native
- detect when another view is touched - dragging with PanResponder in react native
- Full text is not showing in alert dialog in react native
- React Native Text color not working
- React Native Testing Library doesn't find text even though its in debug
- React native layout fixed width and stretch remaining space
- React Bootstrap adjust width of text inputs
- Android - Hide text cursor input React Native
- uri image is not rendering in React native Image view
- Add text inside doughnut chart from chart js-2 in react
More Query from same tag
- Action creators handling axios get.request with state access for param
- useRef - use ref to handler function inside functional component?
- UseEffect multiple re-renders
- What is difference between lifecycle method and useEffect hook?
- Axios with Redux Saga not sending form data when insert and put to nodeJs+express api
- Using external styles and multiple class names with webpack css loader
- Download file after secure link has been returned
- React Material UI - Override using withStyles
- Align an image in the middle
- How to get values from same input type from multiple fields in react?
- React.js: How to render new message at the bottom?
- Are the components returned from higher-order components called closures?
- Typescript: initialize an event listener within a class
- ReactJs : npm erorr with respect to node-sass
- Create multiple resources from the same endpoint in React Admin to have different filters applied
- Testing React form components
- how to make padding for Drawer in react js material ui
- React - How to detect Page Refresh and Redirect user
- React component continuously updating
- How to debounce user input for controlled component without lag
- Redux save store as global variable on React app
- How to save the state of drag and drop rows in React?
- Why is a horizontal line created, when user inputs whitespace?
- Modify component specific state ( not registered on redux store ) inside a saga
- Typescript React Error
- ReactJS Module build failed: SyntaxError: Unexpected token
- Conditional onClick from ternary
- Inheritance of React.Component in TypeScript
- How to sign a cookie on a rails API based on a parameter sent by a react front end app?
- How to force a re-render of a hook from a parent component in react?