最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to Horizontally Center React Native Flexbox Text that Wraps across Multiple Lines - Stack Overflow

programmeradmin2浏览0评论

In React Native, using flexbox, text in my <Text> ponents are centered Horizontally (and vertically).

This works as expected -- if line of text is short enough that it does not need to wrap onto the next line.

But, when given a long text string, that wraps across multiple lines, the rendered text does not center horizontally !
Instead the wrapped text is aligned to the left edge.

How can I force wrapped lines of text to center horizontally ?

This horizontally centers shortText, but not longText:

import React from 'react';
import { View, Text, StyleSheet } from 'react-native';

class Deck extends React.Component {

  render() {
    const shortText = 'Centered!';  
    const shortText = 'This very long line of text does NOT Center. Instead the wrapped text is aligned to the left edge of the container.';         

    return (
      <View style={styles.container}>
        <View>
          <Text style={styles.titleText}> {shortText} </Text>
          <Text style={styles.titleText}> {longText}  </Text>
        </View>   
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  titleText: {
    fontSize: 27,
    flexWrap: 'wrap', 
    alignSelf: 'center',
  },
});    

Note: I find plenty of Stack Overflow questions/answers, and blog posts
for Vertical centering, and Horizontal centering when the text does Not wrap onto multiple "lines".
But I have been unable to find any for centering horizontal text that wraps to multiple lines.

Also: note that React Native implements "limited style inheritance" for text which differs slightly from the css standard. For example, <Text>inherits color and 'fontSizeproperties only from parentTextponents, but notView` ponents.
Also, the Flexbox layout implementation for React Native differs slightly from the CSS standard.
I don't know if React Native differences affect horizontally centered wrapped text as well, or not.

In React Native, using flexbox, text in my <Text> ponents are centered Horizontally (and vertically).

This works as expected -- if line of text is short enough that it does not need to wrap onto the next line.

But, when given a long text string, that wraps across multiple lines, the rendered text does not center horizontally !
Instead the wrapped text is aligned to the left edge.

How can I force wrapped lines of text to center horizontally ?

This horizontally centers shortText, but not longText:

import React from 'react';
import { View, Text, StyleSheet } from 'react-native';

class Deck extends React.Component {

  render() {
    const shortText = 'Centered!';  
    const shortText = 'This very long line of text does NOT Center. Instead the wrapped text is aligned to the left edge of the container.';         

    return (
      <View style={styles.container}>
        <View>
          <Text style={styles.titleText}> {shortText} </Text>
          <Text style={styles.titleText}> {longText}  </Text>
        </View>   
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  titleText: {
    fontSize: 27,
    flexWrap: 'wrap', 
    alignSelf: 'center',
  },
});    

Note: I find plenty of Stack Overflow questions/answers, and blog posts
for Vertical centering, and Horizontal centering when the text does Not wrap onto multiple "lines".
But I have been unable to find any for centering horizontal text that wraps to multiple lines.

Also: note that React Native implements "limited style inheritance" for text which differs slightly from the css standard. For example, <Text>inherits color and 'fontSizeproperties only from parentTextponents, but notView` ponents.
Also, the Flexbox layout implementation for React Native differs slightly from the CSS standard.
I don't know if React Native differences affect horizontally centered wrapped text as well, or not.

Share Improve this question edited Apr 23, 2018 at 1:02 Michael Benjamin 372k110 gold badges613 silver badges730 bronze badges asked Apr 20, 2018 at 20:54 SherylHohmanSherylHohman 18k18 gold badges93 silver badges96 bronze badges 1
  • 2 Does adding textAlign: "center" to the <Text>'s style prop work? – CRice Commented Apr 20, 2018 at 20:58
Add a ment  | 

1 Answer 1

Reset to default 9

You can set the textAlign property like this:

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

You can see the docs for the textAlign property here: https://reactnative.dev/docs/text-style-props#textalign

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论