如何在ReactNative的水平和垂直中心文本?

我在rnplay.org中有一个示例应用程序,其中justifyContent=“中心”和alignItems=“中心”是不工作的: https://rnplay.org/apps/AoxNKQ

文字应该居中。 为什么在顶部的文本(黄色)和父容器之间有一个边距?

代码:

'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;

从标题样式中删除height, justification content和alignItems。它将使文本垂直居中。添加textAlign: 'center',它将使文本水平居中。

  headline: {
    textAlign: 'center', // <-- the magic
    fontWeight: 'bold',
    fontSize: 18,
    marginTop: 0,
    width: 200,
    backgroundColor: 'yellow',
  }

在图像组件上设置这些样式:

{
  textAlignVertical: "center",
  textAlign: "center"
}

已经回答了,但我想在这个主题上添加更多内容,并根据您的用例添加不同的方法。

你可以添加adjustsFontSizeToFit={true}(目前没有文档)到文本组件来自动调整父节点内的大小。

  <Text adjustsFontSizeToFit={true} numberOfLines={1}>Hiiiz</Text>

您还可以在文本组件中添加以下内容:

<Text style={{textAlignVertical: "center",textAlign: "center",}}>Hiiiz</Text>

或者你可以将以下内容添加到Text组件的父组件中:

<View style={{flex:1,justifyContent: "center",alignItems: "center"}}>
     <Text>Hiiiz</Text>
</View>

或两个

 <View style={{flex:1,justifyContent: "center",alignItems: "center"}}>
     <Text style={{textAlignVertical: "center",textAlign: "center",}}>Hiiiz</Text>
</View>

或者三者都有

 <View style={{flex:1,justifyContent: "center",alignItems: "center"}}>
     <Text adjustsFontSizeToFit={true} 
           numberOfLines={1} 
           style={{textAlignVertical: "center",textAlign: "center",}}>Hiiiz</Text>
</View>

这一切都取决于你在做什么。你也可以查看我关于这个主题的完整博客文章

https://medium.com/@vygaio/how-to-auto-adjust-text-font-size-to-fit-into-a-nodes-width-in-react-native-9f7d1d68305b

const styles = StyleSheet.create({
        navigationView: {
        height: 44,
        width: '100%',
        backgroundColor:'darkgray',
        justifyContent: 'center', 
        alignItems: 'center' 
    },
    titleText: {
        fontSize: 20,
        fontWeight: 'bold',
        color: 'white',
        textAlign: 'center',
    },
})


render() {
    return (
        <View style = { styles.navigationView }>
            <Text style = { styles.titleText } > Title name here </Text>
        </View>
    )

}

这是一个同时进行水平和垂直对齐的例子

<View style={{width: 200, flexDirection: 'row',alignItems: 'center'}}>
     <Text style={{width: '100%',textAlign: 'center'}} />
</View>
textAlignVertical: "center"

才是真正的魔法。

水平和垂直中心对齐

<View style={{flex: 1, justifyContent: 'center',alignItems: 'center'}}>
     <Text> Example Test </Text>
</View>

你可以在文本组件上使用alignSelf属性

{ alignSelf : "center" }

除了其他答案中提到的用例之外:

要将文本置于BottomTabNavigator的特定用例的中央,请记住将showIcon设置为false(即使TabNavigator中没有图标)。否则,文本将被推到Tab的底部。

例如:

const TabNavigator = createBottomTabNavigator({
  Home: HomeScreen,
  Settings: SettingsScreen
}, {
  tabBarOptions: {
    activeTintColor: 'white',
    inactiveTintColor: 'black',
    showIcon: false, //do this
    labelStyle: {
      fontSize: 20,
      textAlign: 'center',
    },
    tabStyle: {
      backgroundColor: 'grey',
      marginTop: 0,
      textAlign: 'center',
      justifyContent: 'center',
      textAlignVertical: "center"
    }
  }

无论你在你的页面中有文本组件,你只需要设置文本组件的样式。

 <Text style={{ textAlign: 'center' }}> Text here </Text>

你不需要添加任何其他样式属性,这是壮观的魔术,它会设置你的文本在你的UI的中心。

可以使用以下属性:{{alignItems: 'center'}}因为您使用的是item <Text>而不是"string"。

<View style={{alignItems: 'center'}}>
 <Text> Hello i'm centered text</Text>
</View>

简单的添加

textAlign: "center"

在样式表中,就是这样。 希望这能有所帮助。

编辑:“中心”

首先在父视图中添加 flex: 1、 justifyContent:“中心”, alignItems:“中心”

然后在文本中添加 textAlign:“中心”

使用

文本对齐:居中

看风景

在父视图中设置

justifyContent:center

在子视图中alignSelf:center

你可以使用两种方法:

To make text align center horizontally, apply this Property (textAlign:"center"). Now to make the text align vertically, first check direction of flex. If flexDirection is column apply property (justifyContent:"center") and if flexDirection is row is row apply property (alignItems : "center") . To Make text align center apply same property (textAlign:"center"). Now to make it align vertically make the hieght of the <Text> </Text> equal to view and then apply property (textAlignVertical: "center")...

它很可能会起作用……

好的,所以这是一个基本问题,不要担心这个 只需编写<View>组件,并将其环绕<Text>组件

<View style={{alignItems: 'center'}}>
<Text> Write your Text Here</Text>
</View>

Alignitems:center是一个道具,用来在十字上对齐物品


“中心”是一个道具,用于将物品置于主轴的中心

<View style={{ backgroundColor: 'blue', justifyContent: 'center' }}>
  <Text style={{ fontSize: 25, textAlign: 'center' }}>A</Text>
</View>

标题:{ textAlign: 'center',fontWeight: 'bold'}

在许多情况下,ReactNative支持CSS样式属性,命名为骆驼式,而不是标准的CSS dasherized-lisp样式(aka kebab-case)

这与React/JSX相同。

CSS属性text-align有直接等价的:textAlign

因此,通常值得尝试CSS属性,转换为camelCase。

<View style={{alignSelf:'center'}}>
<Text>React Native</Text>
</View>