ChatGPT解决这个技术问题 Extra ChatGPT

ReactNative:如何使文本居中?

如何在 ReactNative 中将文本在水平和垂直方向居中?

我在 rnplay.org 中有一个示例应用程序,其中 justifyContent="center"alignItems="center" 不起作用: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;
像这样: rnplay.org/apps/1hbnSA
这不是水平居中的
好的,所以这个:rnplay.org/apps/1hbnSA,已更新
哇... textAlign ?我知道这会很愚蠢....您应该将其发布为答案

D
David Schumann

headline' 样式中删除 heightjustifyContentalignItems。它将垂直居中文本。添加 textAlign: 'center' 它将使文本水平居中。

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

不能与 width 一起使用,除非您使用 justifyContent: 'center', alignItems: 'center', 将其包装在 <View>
你的魔法没有奏效,但 width: "100%", 奏效了
我只是通过将其更改为“text-Align:center;”来使其工作。
R
Ricardo

已经回答,但我想根据您的用例添加更多关于该主题的内容以及不同的方法。

您可以将 adjustsFontSizeToFit={true}(当前未记录)添加到 Text 组件以自动调整父节点内的大小。

  <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


这应该是意料之中的:Hiiiz
伙计,你节省了我的 1.5 小时。我试图做同样的事情,但直到这个答案成为:textAlignVertical: "center", textAlign: "center" ,作为 <Text/>组件样式。
textAlignVertical 仅适用于 Android。这些解决方案都没有垂直对齐我的文本,只有水平对齐。
父组件中的 justifyContent: "center" 是魔法酱。谢谢!
m
mocansergiu93

将这些样式设置为图像组件: { textAlignVertical: "center", textAlign: "center" }


M
Muzammil
textAlignVertical: "center"

是真正的魔法。


M
Mahdi Bashirpour

水平和垂直居中对齐

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

唯一对我有用的解决方案是在 中居中 ,不仅水平,而且垂直。谢谢!
M
Mahdi Bashirpour

这是同时水平和垂直对齐的示例

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

感谢您提示内联样式必须包含在 {{double-curly-braces}} 中。
O
Omar bakhsh

可以使用以下属性:{{alignItems: 'center'}},因为您使用的是项目 <Text> 而不是“字符串”。

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

即使这可能是问题的解决方案,请添加更多解释,以便我们所有人都可以学习。
请改进它,因为您使用两个星号来循环主要更正,但不清楚,另一部分令人困惑
D
DarkSuniuM

您可以在 Text 组件上使用 alignSelf 属性

{ alignSelf : "center" }

A
Antier Solutions

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

H
Hardik Virani

无论您在页面中的哪个位置使用 Text 组件,您只需设置 Text 组件的样式即可。

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

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


G
Guillermo García

简单添加

textAlign: "center"

在你的 styleSheet 中,就是这样。希望这会有所帮助。

编辑:“中心”


C
Chris Catignani

在父视图中设置

justifyContent:center

并在子视图中 alignSelf:center


密钥应采用驼峰式 justifyContentalignSelf
S
Shahmir Khan

好的,所以这是一个基本问题,不用担心,只需编写 组件并将其包装在 组件周围

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

alignitems:center 是用于在横轴上居中项目的道具

justifycontent:'center' 是用于在主轴上居中项目的道具


o
onosendai

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

要在 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"
    }
  }

佚名

首先在父视图中添加 flex:1, justifyContent: 'center', alignItems: 'center'

然后在文本中添加 textAlign:'center'


S
Sanjib Suna

用过的

文本对齐:居中

在视图中


D
David Schumann
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>
    )

}

G
General Grievance

您可以为此使用两种方法...

要使文本水平居中对齐,请应用此属性 (textAlign:"center")。现在要使文本垂直对齐,首先检查 flex 的方向。如果 flexDirection 是列应用属性 (justifyContent:"center") 并且如果 flexDirection 是行是行应用属性 (alignItems : "center")。要使文本居中对齐,请应用相同的属性 (textAlign:"center")。现在使其垂直对齐,使 的高度等于视图,然后应用属性(textAlignVertical:“center”)...

最有可能它会工作......


关注公众号,不定期副业成功案例分享
关注公众号

不定期副业成功案例分享

领先一步获取最新的外包任务吗?

立即订阅