I have a Text element and need to position another Text element directly below it. However, there appears to be space at the bottom of the Text element that keeps me from doing this. How can I remove the space at the bottom of the Text element with "14.45366" so that the text element with "Current Balance" can then be positioned so it is almost touching "14.45366"?
The purple colored background is to show the amount of space that can be found within the Text element itself.
<Text style={{ fontSize: 56, fontFamily: 'TitilliumWeb_600SemiBold', textAlign: 'center', backgroundColor: 'purple' }}>14.45366</Text>
<Text style={{ fontSize: 16, fontFamily: 'TitilliumWeb_400Regular', textAlign: 'center', color: '#979797' }}>Current Balance</Text>
I did try adding lineHeight to the styling but it cuts the text off from the top.
<Text style={{ fontSize: 56, fontFamily: 'TitilliumWeb_600SemiBold', textAlign: 'center', backgroundColor: 'purple', lineHeight: 56 }}>14.45366</Text>
Related
I want my text and button at the top of the screen, but both the elements have a few gaps between, so I added marginBottom to my text and then that text went to the top. But I want that button to be at the top so I added marginBottom to the button too, but now it's not going to the top.
How can I position it to the top? Where is the style issue coming from? Please, explain my mistake as well.
import { StyleSheet, Text, View, Button } from 'react-native';
export default function App() {
return (
<View style={styles.container}>
<Text style={{ margin: 16, marginBottom: '80%', backgroundColor: 'black' }}>Hello World</Text>
<View style={{ marginBottom: '80%' }}>
<Button title='Click me' />
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
You're using alignItems: 'center' and justifyContent: 'center'.
These two properties combined make the contents of container be positioned in the center. alignItems:center vertically centers the contents, and justifyContent:center horizontally centers the contents.
Since Text is the upper child element, giving it a marginBottom will space it out from the View element containing the Button. If you compare your screen with and without that marginBottom on the Text you'll notice that the button also moves slightly to the bottom if you include the marginBottom on the Text.
So in short, your code is doing exactly what you tell it to do: container tries to position all children to its center.
To fix this, you might want to remove the container:{ alignItems:'center', } property. This will most likely push all your elements to the top of the container. After that you might simply use marginTop on your Text element to push the contents down.
Change both marginBottom %80 to "auto".
return (
<View style={styles.container}>
<Text
style={{ margin: 16, marginBottom: "auto", backgroundColor: "white" }}
>
Hello World
</Text>
<View style={{ marginBottom: "auto" }}>
<Button title="Click me" />
</View>
</View>
);
This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 2 years ago.
I want to center the second piece of text. The first piece has an icon that takes up part of the width. The second text doesn't take up the full width of the screen, and so when I apply the textAlign to center, the text center should consider its width.
There is another text below the first that takes the full width, so it's really aligned in the center of the screen instead of being center relative the the first piece of text above it.
code:
<View style={{ marginBottom: 25 }}>
<View style={{ flex: 1, flexDirection: "row", alignItems: "center", marginTop: 15 }}>
<MaterialIcons onPress={() => console.log("funciona por favor!!")} name="close" size={20} style={{ color: "#FFF" }} />
<Text style={{ flex: 1, color: "#FFF", fontSize: 24, textAlign: "center" }}>Configurações</Text>
</View>
<Text style={{ color: COLORS.white1, fontSize: 24, marginTop: 0, alignSelf: "center" }}>Configurações</Text>
</View>
What I tried:
The icon has 30px of width, so I tried a negative value margin for the text and to center it, but it covers the icon, and this icon needs to be pressed, so the press doesn't work.
Thanks for the help, I hope I have been clear in exposing the problem.
You can add a margin to the right of the content to offset the amount the text it pushed from the right.
<View style={{ marginBottom: 25, backgroundColor: 'grey' }}>
<View style={{ flex: 1, flexDirection: "row", marginTop: 15 }}>
<MaterialIcons name="close" size={20} style={{ color: "#FFF" }} />
<Text style={{ flex: 1, color: "#FFF", fontSize: 24, textAlign: "center", marginRight: 20 }}>Configurações</Text>
</View>
<Text style={{ color: '#fff', fontSize: 24, marginTop: 0, alignSelf: "center" }}>Configurações</Text>
</View>
Make your button a grid:
div.button {
background:blue;
padding:4px;
color:white;
font-family:"Verdana";
width:50vw;
display:grid;
grid-template-columns: 5% auto 5%;
}
div.button div.caption {
text-align:center;
}
<div class="button">
<div class="left">ICON</div>
<div class="caption">Your caption here</div>
<div class="right"></div>
</div>
Using this approach, you define three columns, put your icon into the leftmost one, the caption into the middle one, and leave one column free which is as wide as the left one, making the center column actually being centered.
You wrap your close icon in absolute view and set the left position accordingly. In this way, your text will be aligned with the below text
<View style={{position:'absolute',left:0}}>
<MaterialIcons onPress={() => console.log("funciona por favor!!")} name="close"
size={20} style={{ color: "#FFF" }} />
</View>
I am new to RN and I have this issue: My page will have 3 sections with titles on the left side. To maximize space I want these titles to be rotated by -90 degrees. My problem is that the words will break and wrap to the next line on the longer titles.
I can add numberOfLines={1} and that prevents the wrapping. But that now truncates the text and adds an ellipsis. Is there a way I can do both (rotate the text and NOT have the text truncate)?
<View style={styles.budget}>
<View style={styles.boxTitle}>
<Text style={styles.titleText} numberOfLines={1}>Transform</Text>
</View>
<View style={styles.boxBody}>
<View style={styles.budgetBar}>
</View>
</View>
</View>
boxTitle: {
borderColor: 'red',
borderWidth: 2,
width: '15%',
justifyContent: 'center',
flexWrap: 'nowrap',
},
titleText: {
transform: [{ rotate: '-90deg'}],
fontSize: 20,
fontWeight: 'bold',
textDecorationLine: 'underline',
},
boxBody: {
borderWidth: 2,
width: '85%',
flexDirection: 'row'
},
Basically, i had two buttons that had a top and bottom border, they are inside a view, like this
<View>
<MyTab>Sobre</MyTab>
<MyTab>Portfólio</MyTab>
</View>
This view is inside another one. This main container applies a padding arround the entire app. How can i make this border bottom and top to overlap this main padding and make the border to touch the end of the screen
My css applied to the buttons are
navigationTabs: {
fontFamily: Fonts.main.REGULAR,
width: '50%',
borderRadius: 0,
paddingTop: ScreenSize.getSpaces(10),
paddingBottom: ScreenSize.getSpaces(10),
marginBottom: ScreenSize.getSpaces(10),
textAlign: 'center',
borderColor: Colors.TEXT_DARK,
borderTopWidth: 0.5,
borderBottomWidth: 0.5,
},
I'd like to have a button with an image and a text centered.
The button is placed in the bottom of the screen.
I have this code :
<TouchableOpacity style={Styles.oneButton}>
<View style={Styles.oneButtonView}>
<Text style={Styles.payTextDollar}>
<Image
style={Styles.imageButton}
source={props.image}
/>
Pay
</Text>
</View>
</TouchableOpacity>
oneButton: {
height: 60,
backgroundColor: '#e41c23',
}
oneButtonView: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
height: 20,
justifyContent: 'center',
}
payTextDollar: {
flex: 1,
color: '#ffffff',
fontSize: 20,
fontFamily: 'MetaOffc-Light',
textAlign: 'center',
}
imageButton: {
marginTop: 6,
}
this works very well of iOS, however, the image is really small on Android, like so : https://i.stack.imgur.com/jFWAy.png
Do you know anything about this behaviour ?
Do you know if there's another solution to have the image and the text centered in the button like so : https://i.stack.imgur.com/geBSz.png
Thanks !
What are the styles you have set on your image?
Also you shouldn't have to nest image inside of text. I'd pull that out and put it before text.