¿Cómo centrar el texto en ReactNative tanto en horizontal como en vertical?
Tengo una aplicación de ejemplo en rnplay.org donde justifyContent = "center" y alignItems = "center" no funciona: https://rnplay.org/apps/AoxNKQ
El texto debe estar centrado. ¿Y por qué hay un margen en la parte superior entre el texto (amarillo) y el contenedor principal?
Código:
'use strict';
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
Image,
View,
} = React;
var SampleApp = React.createClass({
render: function() {
return (
<View style={styles.container}>
<View style={styles.topBox}>
<Text style={styles.headline}>lorem ipsum{'\n'}ipsum lorem lorem</Text>
</View>
<View style={styles.otherContainer}>
</View>
</View>
);
}
});
var styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
backgroundColor: 'red',
justifyContent: 'center',
alignItems: 'center',
},
topBox: {
flex: 1,
flexDirection: 'row',
backgroundColor: 'lightgray',
justifyContent: 'center',
alignItems: 'center',
},
headline: {
fontWeight: 'bold',
fontSize: 18,
marginTop: 0,
width: 200,
height: 80,
backgroundColor: 'yellow',
justifyContent: 'center',
alignItems: 'center',
},
otherContainer: {
flex: 4,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'green',
},
});
AppRegistry.registerComponent('SampleApp', () => SampleApp);
module.exports = SampleApp;
reactjs
flexbox
react-native
itinerario
fuente
fuente

Respuestas:
Desde
headline'estilo eliminarheight,justifyContentyalignItems. Centrará el texto verticalmente. AgreguetextAlign: 'center'y centrará el texto horizontalmente.fuente
widthmenos que lo envuelva<View>con unjustifyContent: 'center', alignItems: 'center',Ya respondí, pero me gustaría agregar un poco más sobre el tema y diferentes formas de hacerlo dependiendo de su caso de uso.
Puede agregar
adjustsFontSizeToFit={true}(actualmente no documentado) aTextComponente para ajustar automáticamente el tamaño dentro de un nodo primario.También puede agregar lo siguiente en su componente de texto:
O puede agregar lo siguiente en el elemento primario del componente Texto:
o ambos
o los tres
Todo depende de lo que estés haciendo. También puede consultar mi publicación de blog completa sobre el tema
https://medium.com/@vygaio/how-to-auto-adjust-text-font-size-to-fit-into-a-nodes-width-in-react-native-9f7d1d68305b
fuente
textAlignVertical: "center", textAlign: "center"como <Text /> Component Style.Establezca estos estilos en el componente de imagen: {textAlignVertical: "center", textAlign: "center"}
fuente
Es la verdadera magia.
fuente
Este es un ejemplo de alineación horizontal y vertical simultáneamente
fuente
Alineación central horizontal y vertical
fuente
Puede usar la
alignSelfpropiedad en el componente de textofuente
Donde sea que tenga un
Textcomponente en su página, solo necesita establecer el estilo delTextcomponente.no necesita agregar ninguna otra propiedad de estilo, esta es una magia espectacular que le colocará el texto en el centro de su interfaz de usuario.
fuente
Añadir simple
en su hoja de estilo, eso es todo. Espero que esto ayude.
editar: "centro"
fuente
Se puede usar la siguiente propiedad:
{{alignItems: 'center'}}fuente
usado
textalign: center
a la vista
fuente
Establecer en vista principal
y en vista infantil alignSelf: center
fuente
justifyContentyalignSelfAdemás de los casos de uso mencionados en las otras respuestas:
Para centrar el texto en el caso de uso específico de un BottomTabNavigator , recuerde establecer showIcon en falso (incluso si no tiene iconos en el TabNavigator). De lo contrario, el texto se empujará hacia la parte inferior de la pestaña.
Por ejemplo:
fuente
fuente
primero agregue en la vista principal flex: 1, justifyContent: 'center', alignItems: 'center'
luego, en el texto, agregue textAlign: 'center'
fuente