Navigate back to the homepage

how to center text in react native?

Infinitbility
React Native
July 9th, 2022 · 1 min read
how to center text in react native?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To center text in react native, just use the textAlign css property it will center text horizontaly and for vertical center flex: 1, justifyContent: "center", on upper element.

Today, I’m going to show you How do I center text in react native, as above mentioned here, I’m going to use the textAlign and justifyContent css property to make text center.

Let’s start today’s tutorial how do you center text in react native?

In this example, we will do

  1. take sample string state
  2. create container element with flex: 1, justifyContent: "center", css.
  3. create paragraph element with textAlign: 'center', css.

Let’s write code…

1import React, { useState, useEffect } from 'react';
2import { Text, View, StyleSheet } from 'react-native';
3
4const App = () => {
5 const [str, setStr] = useState('Hi Friends, Welcome to infinitbility');
6
7 return (
8 <View style={styles.container}>
9 <Text style={styles.paragraph} >{str}</Text>
10 </View>
11 );
12}
13
14const styles = StyleSheet.create({
15 container: {
16 flex: 1,
17 backgroundColor: '#ecf0f1',
18 justifyContent: "center",
19 },
20 paragraph: {
21 margin: 24,
22 fontSize: 18,
23 fontWeight: 'bold',
24 textAlign: 'center',
25 },
26});
27
28export default App;

As mentioned above, we are taking the example of a string state, used center text css style and show the message at the center of the app screen.

Let’s check the output.

React Native, center text example
React Native, center text example

Try it yourself

I hope it’s help you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to check empty object in node js?

how to check empty object in node js?

An example of check empty object in node js with step by step guide.

July 9th, 2022 · 1 min read
how to check empty object in react js?

how to check empty object in react js?

An example of check empty object in react js with step by step guide.

July 9th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]