React Native: How to align inline text? - react-native

I want to do this, where both of the texts are on 1 horizontal line and one of them is in the center and the other is on the right:
This is what I have, ignore the colors (it doesn't work at all):
styles:
rowLabelText: {
color: "#0B1219",
fontFamily: Fonts.Knockout31JuniorMiddleWeight,
fontSize: 16.0,
},
Markup:
<View style={{flexDirection: 'row', height: 30, flexWrap: 'wrap', backgroundColor: 'green'}}>
<View style={{ flex: 1, backgroundColor: 'red', justifyContent: 'center', alignSelf: 'center'}}>
<Text style={styles.rowLabelText}>
adjkfdasjkfaskj
</Text>
</View>
<View style={{ flex: 1, backgroundColor: 'blue', justifyContent: 'center', alignSelf: 'flex-end' }}>
<Text style={styles.rowLabelText}>
adjkfdasjkfaskj
</Text>
</View>
</View>
I am having trouble. Could someone assist me?

It looks like your problem is with with alignSelf you want to use alignItems.
This is how your code will look like.
Your View:
<View style={styles.textContainer}>
<View style={styles.leftContainer}>
<Text style={styles.rowLabelText}>
adjkfdasjkfaskj
</Text>
</View>
<View style={styles.rightContainer}>
<Text style={styles.rowLabelText}>
adjkfdasjkfaskj
</Text>
</View>
</View>
Your styles:
textContainer:{
flexDirection: 'row',
height: 30,
backgroundColor: 'green'
},
leftContainer:{
flex: 1,
justifyContent: 'center',
alignItems:'center',
backgroundColor: 'red',
},
rightContainer:{
flex: 1,
justifyContent: 'center',
alignItems: 'flex-end',
backgroundColor: 'blue',
},
rowLabelText: {
color: "#0B1219",
fontSize: 16.0,
},

One way to center text with respect to the entire width of the screen while also having some text to the right side of the centered text is to use absolute positioning on the right text.
By using absolute positioning on the right text, it will not affect the position of the centered text.
<View style={{flexDirection: 'row'}}>
<View style={{flex:1, alignItems: 'center', backgroundColor: 'red'}}>
<Text>50%</Text>
</View>
<View style={{position:'absolute', right: 20, backgroundColor: 'blue'}}>
<Text>+$0.80</Text>
</View>
</View>

Related

Dynamic horizontal rule with centered text react native

I would like to have the grow horizontally not vertically. My current code is this
<View style={{ flexDirection: 'row', alignItems: 'center', backgroundColor: theme.backgroundColor }}>
<View style={{ flex: 1, height: 1, backgroundColor: 'white' }} />
<View>
<Text style={{ width: 50, textAlign: 'center', color: 'white' }}>Here is the problem</Text>
</View>
<View style={{ flex: 1, height: 1, backgroundColor: 'white' }} />
</View>
but this grows vertically any ideas.
You are limiting the width of the Text component, thus the text will not have enough space. Therefore, it will be breaking into several lines.
If we remove the fixed width, it will use the available vertical space.
<View style={{ flexDirection: 'row', alignItems: 'center', backgroundColor: theme.backgroundColor }}>
<View style={{ flex: 1, height: 1, backgroundColor: 'white' }} />
<View>
<Text style={{ textAlign: 'center', color: 'white' }}>Here is the problem</Text>
</View>
<View style={{ flex: 1, height: 1, backgroundColor: 'white' }} />
</View>
Before change
After change

Flexbox alignment issue in react-native

I'm trying to align the contents of a View component into left and right side of the screen. Unfortunately, whatever I do, I can only align the parent View and not the child View components.
In the snippet below, item is the Parent View, leftIcon and title are supposed to be aligned together in the left and rightArrow is supposed to be aligned in the right.
<TouchableOpacity onPress={() => navigation.navigate(`${item.page}`)}>
<View style={styles.item}>
<Text style={styles.leftIcon}>a</Text>
<Text style={styles.title}>{item.title}</Text>
<Text style={styles.rightArrow}> > </Text>
</View>
</TouchableOpacity>
const styles = StyleSheet.create({
item: {
//backgroundColor: '#f9c2ff',
paddingTop: 7,
marginVertical: 4,
marginHorizontal: 16,
flex: 1,
flexDirection: 'row',
},
leftIcon: {
fontSize: 25,
paddingRight: '5%',
alignSelf: 'flex-start',
},
title: {
fontSize: 25,
alignSelf: 'flex-start',
},
rightArrow: {
fontSize: 25,
alignSelf: 'flex-end'
}
});
This might be an incorrect approach, but what can be done to achieve this?
You can wrap leftIcon and title in a view with a flexDirection: 'row' style.
Then add justifyContent: 'space-between' to your item styles.
Example (using inline styles):
<TouchableOpacity>
<View
style={{
marginVertical: 4,
marginHorizontal: 16,
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
}}>
<View style={{ flexDirection: 'row' }}>
<Text
style={{
fontSize: 25,
paddingRight: '5%',
}}>
a
</Text>
<Text style={{ fontSize: 25 }}>Title</Text>
</View>
<Text style={{ fontSize: 25 }}> > </Text>
</View>
</TouchableOpacity>

How to align multiline text in react native

In my screen, I need to split the text into column view and need to align the right side text with the left side text. also, need to show the whole text line by line. Tested with giving some height, width, position: absolute but didn't work. Any idea how to solve this?
I'll post my current code and screenshot for your ref.
Many thanks for your help
<View style={styles.twoCol}>
<View style={styles.leftField}>
<Text style={styles.leftFieldTitle}>Location:</Text>
</View>
<View style={styles.locationRightSide}>
<Text style={styles.rightFieldTitle}>{location}</Text>
</View>
</View>
const styles = StyleSheet.create({
twoCol: {
alignItems: 'center',
flexDirection: 'row',
marginTop: 25,
},
leftField: {
alignItems: 'flex-end',
width: '25%',
},
leftFieldTitle: {
fontWeight: '700',
color: 'black'
},
},
locationRightSide: {
width: '70%',
alignItems: 'flex-start',
paddingLeft: 10
},
rightFieldTitle: {
fontSize: RF(2.8),
color: 'black'
},
)}
Problem solved!!! Thanks everyone!
<View style={{
padding: 10,
flexDirection: "row",
marginLeft: 10,
marginRight: 5,
marginTop: 5,
marginBottom: 3,
justifyContent: "flex-start",
width: '95%',
}} >
<View style={{
position: 'absolute',
marginTop: 10
}}>
<Text style={styles.leftFieldTitle}>Location:</Text>
</View>
<View style={{ marginLeft: '26%' }}>
<Text style={styles.rightFieldTitle}>{location}</Text>
</View>
</View>

How to align a View to center without changing children's position?

<View style={{height: '100%', width: '100%'}}>
{OtherContent}
<ScrollView>
<View style={{flexDirection: 'row', flexWrap: 'wrap', padding: 20}}>
{children}
</View>
<ScrollView>
</View>
I want the View inside ScrollView to be in center of the screen without changing it's children position
Children:
<View style={Styles.docsContainer}>
{arr.map((document, index) => {
return (
<TouchableOpacity key={index} style={[Style.docStyle} onPress={null}>
<Icon name='file-text-o'/>
<Text>{document.name}</Text>
</TouchableOpacity>
);
})}
</View>
const Styles: {
docsContainer: {
flexDirection: 'row',
flexWrap: 'wrap',
padding: 20,
},
docStyle: {
alignItems: 'center',
padding: 20,
margin: 5,
marginBottom: 10,
borderRadius: 8,
width: 170
}
}
Indeed, we don't need parent View, we just need scrollView as flewGrow 1 which makes scrollView have full height and width as per device.
My approach:
<ScrollView contentContainerStyle={{ flexGrow: 1, justifyContent: 'center', alignItems: 'center' }}>
<View style={{
alignItems: 'center',
justifyContent: 'center',
flex: 1,
}}>
<Text>mahesh nandam</Text>
</View>
</ScrollView>
Try this.
The Viewis your ScrollView's immediate children. So you can style ScrollView using contentContainerStyle and align it in center like this.
<View style={{height: '100%', width: '100%'}}>
<ScrollView
contentContainerStyle={{
flexGrow: 1,
justifyContent: 'center',
alignItems: 'center',
}}
>
<View style={{flexDirection: 'row', flexWrap: 'wrap', padding: 20}}>
{children}
</View>
<ScrollView>
</View>
I guess it's what you're loooking for.
Center Horizontally
If you only want to center the View horizontally, you can set alignItems: 'center' to ScrollView's contentContainerStyle.
<View style={{ height: '100%', width: '100%' }}>
<Text>Other content</Text>
<ScrollView contentContainerStyle={{ alignItems: 'center' }}>
<View style={{ flexDirection: 'row', flexWrap: 'wrap', padding: 20, backgroundColor: 'red' }}>
<Text>children</Text>
</View>
</ScrollView>
</View>
The inner View is highlighted in red.
Center Horizontally and Vertically
In this case, you can set { flex: 1, justifyContent: 'center', alignItems: 'center' } for the same contentContainerStyle.
<View style={{ height: '100%', width: '100%' }}>
<Text>Other content</Text>
<ScrollView contentContainerStyle={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<View style={{ flexDirection: 'row', flexWrap: 'wrap', padding: 20, backgroundColor: 'red' }}>
<Text>children</Text>
</View>
</ScrollView>
</View>
If what you expect is different layout than in either of the screenshots, please share a simple sketch of the layout.

How to vertically stretch two Views that are in a column formation - React Native Flex?

I have this view, where there are two Views in a column:
<View style={styles.container}>
<View style={styles.content}>
<View style={{backgroundColor: 'orange'}}>
<Text>Test</Text>
</View>
<View style={{backgroundColor: 'yellow'}}>
<Text>Test2</Text>
</View>
</View>
</View>
This is my Styles:
container: {
flexDirection: 'row',
backgroundColor: 'white',
justifyContent:'center',
alignItems:'center',
height: 80,
margin: 8,
},
content: {
flex:1,
alignItems: 'stretch',
flexDirection: 'column',
},
Here is a ScreenShot of what it looks with the above code.
How can I get the orange and yellow View to expand vertically equally, without manually defining the height for each of them?
The Text inside should be centered, but left-aligned.
Add flex: 1 to each cell to make them expand and add justifyContent: 'center' to make the text vertically centered. Like this:
<View style={styles.container}>
<View style={styles.content}>
<View style={{ backgroundColor: 'orange', flex: 1, justifyContent: 'center' }}>
<Text>Test</Text>
</View>
<View style={{ backgroundColor: 'yellow', flex: 1, justifyContent: 'center' }}>
<Text>Test2</Text>
</View>
</View>
</View>