question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

textAlign does not work for components wrapped in a View inside Text

See original GitHub issue

Description

textAlign style prop does not work when the components nested in a Text are wrapped in a View.

This happens only on iOS and looks like it broke in 0.54.2 version.

Environment

OS: macOS Sierra 10.12.6 Node: 8.9.1 Yarn: 1.3.2 npm: 5.6.0 Watchman: 4.7.0 Xcode: Xcode 9.0 Build version 9A235 Android Studio: Not Found

Packages: (wanted => installed) react: ^16.3.0-alpha.1 => 16.3.0-alpha.2 react-native: 0.54.2 => 0.54.2

Steps to Reproduce

[Could not use snack to reproduce this issue since it happens only on React Native 0.54.2]

Use following code in your render function

  render() {
    if (Platform.OS === 'ios') {
      return (
        <Text style={{textAlign: 'right'}}>
          <View style={{paddingTop: 50}}>
            <Text>
            {'I should be aligned right'}
            </Text>
          </View>
          </Text>
      );
    }
      return (
        <Text style={{textAlign: 'right'}}>
          <Text>
          {'I should be aligned right'}
          </Text>
        </Text>
      );
  }

Note: View is not supported inside Text on Android. Hence, this code does a platform check and does not nest View inside Text on Android. The issue is seen only when View is nested inside Text i.e., only on iOS.

Expected Behavior

The text should be aligned to the right

Actual Behavior

For iOS, in React Native 0.54.2, the text is aligned to the left.

Android (RN 0.54.2) ✅ iOS (RN 0.54.2) ❌
android54 2 ios54 2

As seen from screenshots below, it works correctly in React Native 0.53.3

Android (RN 0.53.3) ✅ iOS (RN 0.53.3) ✅
android53 3 ios53 3

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
steveccablecommented, May 9, 2018

It doesn’t have to be an empty space. It appears that if the first child node of a Text which has textAlign set is a String or Text, then it respects the alignment, and if it is instead a View or Image, it doesn’t.

I’m not super familiar with the iOS flow for this (or Objective C/Swift in general), but this makes it seem like there may be some sort of inspection going on in the child nodes that is causing this bug. Just a hunch based on the behavior.

Note that because this happens not just with Views but also Images (which are allowed on both platforms, do not display the broken behavior on Android, and will continue to be supported inside of text even after https://github.com/facebook/react-native/commit/6a1b41643a5f5035c61a96263220d11d3462e8f2 ) that this is still a problem going forward.

0reactions
stale[bot]commented, Nov 7, 2018

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React-native: textAlign: 'right' not styling correctly
I had an issue on Android where a string wrapped by two <Text> components was not respecting the textAlign property like so <Text>...
Read more >
Text Align - Tailwind CSS
Control the text alignment of an element using the text-left , text-center , text-right , and text-justify utilities. So I started to walk...
Read more >
text-align-last - CSS: Cascading Style Sheets - MDN Web Docs
The text-align-last CSS property sets how the last line of a block or a line, right before a forced line break, is aligned....
Read more >
Text in Compose - Jetpack - Android Developers
The textAlign parameter allows to set the alignment of the text within a Text composable surface area. By default, Text will select the...
Read more >
Position element at the bottom of the screen using Flexbox in ...
This is the scenario when one element does not follow the rule i the container. ... import { View, StyleSheet, Image, Text, Button...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found