Repository navigation
Description
This issue has already been reported (#18790), but closed for "stalling". As of React Native 0.63.3, the bug still exists.
Description
textAlignVertical works fine when set on the top-most Text component. However, if you have any nested texts (a common use case), you can not override the alignVertical in the child Text component.
React Native version:
System:
OS: Linux 5.4 Manjaro Linux
CPU: (8) x64 Intel(R) Core(TM) i7-8809G CPU @ 3.10GHz
Memory: 1.54 GB / 31.29 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 14.15.0 - /tmp/xfs-2fbb69bb/node
Yarn: 2.2.2 - /tmp/xfs-2fbb69bb/yarn
npm: 6.14.8 - /usr/bin/npm
Watchman: 4.9.0 - /usr/bin/watchman
SDKs:
Android SDK:
API Levels: 28, 29
Build Tools: 28.0.3, 29.0.0, 29.0.2, 29.0.3, 30.0.0, 30.0.1
System Images: android-22 | Google APIs Intel x86 Atom, android-23 | Google APIs Intel x86 Atom, android-29 | Google APIs Intel x86 Atom, android-30 | Google Play Intel x86 Atom
Android NDK: Not Found
IDEs:
Android Studio: 4.1 AI-201.8743.12.41.6858069
Languages:
Java: 1.8.0_265 - /usr/bin/javac
Python: 3.8.6 - /usr/bin/python
npmPackages:
@react-native-community/cli: Not Found
react: 16.13.1 => 16.13.1
react-native: ^0.63.3 => 0.63.3
npmGlobalPackages:
react-native: Not Found
Steps To Reproduce
Create an app which renders the following component:
export default function App() {
return (
<Text>
<Text>This is text</Text>
<Text style={{textAlignVertical: 'top', fontSize: 10}}>sup!</Text>
</Text>
);
}Expected Results
The "sup!" Text node should be aligned to the top edge of the line. Instead, "sup!" is aligned with the baseline:


Activity
dededavida commentedon Jan 27, 2021
hey guys, I created a component that worked for me, I'm sharing
https://github.com/daviddossantos/textinput-multiline-multiplataform
jsamr commentedon Jan 27, 2021
@daviddossantos thanks for sharing but this is unrelated! The bug I reported was on Android and targeted
Textcomponent. Your workaround seems to target iOS and theTextInputcomponent.dededavida commentedon Jan 27, 2021
sorry, I didn't pay attention
fabOnReact commentedon Feb 18, 2021
unluckily @hoda-oz is right
I have been reading https://github.com/facebook/react-native/pull/23195/files and https://github.com/facebook/react-native/pull/8619/files to understand how nested
Textare combined in oneTextViewhttps://github.com/facebook/react-native/blob/28fb41a0ab48cc01d606b64744c84e2ac3805f3f/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java#L42-L47
The code below loops the different text and probably is responsible for aggregating them in 1 TextView.
https://github.com/facebook/react-native/blob/28fb41a0ab48cc01d606b64744c84e2ac3805f3f/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java#L111-L112
each one of the below code-snippets adds a different text property to the
Textsup!using the Android Spans API. Hopefully I will be able to use AlignmentSpan to add the required functionality...More code references
https://github.com/facebook/react-native/blob/28fb41a0ab48cc01d606b64744c84e2ac3805f3f/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java#L174
https://github.com/facebook/react-native/blob/28fb41a0ab48cc01d606b64744c84e2ac3805f3f/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java#L178-L179
https://github.com/facebook/react-native/blob/28fb41a0ab48cc01d606b64744c84e2ac3805f3f/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java#L186
https://github.com/facebook/react-native/blob/28fb41a0ab48cc01d606b64744c84e2ac3805f3f/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java#L193
https://github.com/facebook/react-native/blob/28fb41a0ab48cc01d606b64744c84e2ac3805f3f/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java#L199-L207
https://github.com/facebook/react-native/blob/28fb41a0ab48cc01d606b64744c84e2ac3805f3f/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java#L210
https://github.com/facebook/react-native/blob/28fb41a0ab48cc01d606b64744c84e2ac3805f3f/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java#L213
https://github.com/facebook/react-native/blob/28fb41a0ab48cc01d606b64744c84e2ac3805f3f/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java#L220-L227
https://github.com/facebook/react-native/blob/28fb41a0ab48cc01d606b64744c84e2ac3805f3f/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java#L233
https://github.com/facebook/react-native/blob/28fb41a0ab48cc01d606b64744c84e2ac3805f3f/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java#L235
fabOnReact commentedon Feb 19, 2021
fabOnReact commentedon Feb 19, 2021
fabOnReact commentedon Feb 19, 2021
I further researched and found the AlignmentSpan
as explained in ParagraphSpans they need a new line
\nlikeSpantastic\ntext.as explained in AlignmentSpan.Standard we can use the following Layout.Alignment
ALIGN_CENTERALIGN_OPPOSITEALIGN_NORMALThis are examples of
ALIGN_OPPOSITEThe functionality is not available in AOSP so I should create a CustomSpan which inherits from
AlignmentSpanand changes the text-alignment totop.AlignmentSpanusesmAlignment.name()to retrieve theSpanwith the correct alignmenthttps://github.com/aosp-mirror/platform_frameworks_base/blob/7096deadf4a32768717e74b11eb8e6289a65976f/core/java/android/text/style/AlignmentSpan.java#L95-L98
https://github.com/aosp-mirror/platform_frameworks_base/blob/7096deadf4a32768717e74b11eb8e6289a65976f/core/java/android/text/Layout.java#L529-L581
https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/text/Layout.java;l=440;drc=caabd536e8fea82b888902f359b33e637267966c?q=ALIGN_OPPOSITE&ss=android%2Fplatform%2Fsuperproject
CLICK TO OPEN ANDROID SOURCECODE
jsamr commentedon Feb 22, 2021
fabOnReact commentedon Mar 1, 2021
fabOnReact commentedon Apr 17, 2021
thanks a lot @jsamr
Yes, I believe you are right .. We can use SuperscriptSpan and SubscriptSpan
CLICK TO OPEN TESTS RESULTS
TEST SCENARIO
RESULT
in ReactBaseTextShadowNode
Superscript

Subscript

jsamr commentedon Apr 17, 2021
@fabriziobertoglio1987 That is promising! Although it would be more like super / sub support rather than a fix for this peculiar issue. But a very exciting new feature! Below just a recap on the distinction between super and top.
In CSS, top, bottom are quite different than sub and sup:
What that basically mean is that, if you have a sibling which expands the line box such as a big image,
topwould put the content aligned with the top of the image (which coincide with the line box) whilesuperwould only shift the content a little bit up from the baseline.See this JSFiddle
But honestly, I have no idea what are the specs of React Native inline layout! There certainly are models for native platforms.
fabOnReact commentedon May 2, 2021
I was able to prepare a fix for this issue, the problem is that I don't know how to pass the prop.
The prop
textAlignVertical: 'super'needs to be passed to thespan(the nested/child<Text>sup!</Text>) and not the parent androidTextView(the parent react<Text>This is text</Text>).The class that handle
TextViewattributes isReactTextAnchorViewManagerwhich for example applies the propTEXT_ALIGN_VERTICALand changes the alignment of the parentText.https://github.com/facebook/react-native/blob/cf0a6e9e2734a5927b9dc08303c22477ad274150/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextAnchorViewManager.java#L72
while the second child/nexted text
<Text>This is text<Text>sup!</Text></Text>is just part of the originalTextView, but it is styled differently using an androidSuperscriptSpaninsted of for example CustomLetterSpacingSpanhttps://github.com/facebook/react-native/blob/cf0a6e9e2734a5927b9dc08303c22477ad274150/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java#L176-L180
This properties are applied by the ReactBaseTextShadowNode and are taked from the textAttributes properties listed below
https://github.com/facebook/react-native/blob/cf0a6e9e2734a5927b9dc08303c22477ad274150/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java#L323-L330
I still did not find the logic that adds this properties to the ReactBaseTextShadowNode, but I imagine may be in the ReactTextViewAnchorManager
I will have to further investigate were to add this property, probably I will have to investigate how the style properties are used to add the spans. Thanks a lot for the patience in adding this feature 🙏
8 remaining items
fabOnReact commentedon Dec 22, 2022
As discussed in this comment in the conversation 2022: How can we improve React Native? #528, I'm starting to work on this issue, and I will try to publish a PR. A summary of all the updates will be posted in react-native-community/discussions-and-proposals#495 (comment)
fabOnReact commentedon Jan 3, 2023
@jsamr in react-native we use gravity to align the parent text top/center/bottom
Android Gravity behaves like flex box #35704 (comment)
For this reason a Text with height 250px and textAlignVertical bottom aligns to the bottom (baseline -250px).
CSS behaves different from react-native. The same result is obtained with flex style instead of vertical-align
I think the expected behaviour for this functionality is aligning the text relatively to the lineHeight and not using Gravity (alignment relative to the parent component) https://jsfiddle.net/Lbc0y7ph/
https://www.w3schools.com/cssref/pr_pos_vertical-align.php
I will update my existing solution, which somehow implements the same flex layout logic to nested text (align nested text top/bottom of the parent component).
Making links independently focusable by Talkback (react#33215)
github-actions commentedon Sep 15, 2023
This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.
miallo commentedon Sep 15, 2023
Still an issue
paulschreiber commentedon Nov 16, 2023
Still a problem with 0.72.6.
fabOnReact commentedon Jan 18, 2024
Hello,
Thanks for the issue. I have been contributing to facebook/react-native for 4 years and specialize in the Text/TextInput components. I currently have 58 facebook/react-native PRs:
https://github.com/facebook/react-native/pulls?q=is%3Apr+author%3Afabriziobertoglio1987+
This is the suggested approach from the react-native core team (see this discussion):
I'm publishing several fixes for Text and TextInput component in a separate library react-native-improved.
The advantages would be:
What do you think about this? Should I keep working on this? Thanks
fabOnReact commentedon Jan 18, 2024
I'm the author of #35949 and #35704. The two PRs fix this issue.
ngoclamnn commentedon Apr 11, 2024
still getting this issue when render sub and sup html tag using react-native-render-html
qwertychouskie commentedon Sep 20, 2024
@fabOnReact Both those PRs were closed as stale. Are there new PRs or is the project stalled?
Tirumal5a0 commentedon Jan 9, 2026
I'm also facing the same issue