17

I would like to style text as superscript in React Native. How would this be accomplished? Is there a way to make use of the Javascript sup() string method to return a string as superscript within a <Text> object?

0

10 Answers 10

20

I got this working for me using a view container and flex.

<View style={{flexDirection: 'row', alignItems: 'flex-start'}}>
    <Text style={{fontSize: 20, lineHeight: 30}}>10</Text>
    <Text style={{fontSize: 11, lineHeight: 18}}>am</Text>
</View>

Here is the link to this in action https://repl.it/Haap/0

Cheers!

Sign up to request clarification or add additional context in comments.

1 Comment

I tried your solution but it gives me problems as described in the comments of one of the answers here stackoverflow.com/questions/49536698/…
5

i just do this to achieve superscript in my case

 <View style={{ flexDirection: 'row' }}>
  <Text style={{ fontSize: 30 }}>e</Text>
  <Text style={{ fontSize: 10 }}>x</Text>
 </View>

1 Comment

The "x" needs to be positioned above the "e" though.
3

Just use fontSize, lineHeight, textAlignVertical:

<Text style={{fontSize:20, lineHeight:22}}>
  foo
  <Text style={{fontSize:20 * 1.6, lineHeight:22 * 1.1, textAlignVertical: 'top'}}>
    bar
  </Text>
</Text>

2 Comments

I needed to fix because a special aspect ratio needs between the lineHeights
textAlignVertical is android only!
1

Javascripts sub() function will only surround your text with <sub></sub> tags and they are recognized as text in RN. You would need to build your own function like:

export default class Test extends Component {
    sub = (base, exponent) => {
        return <View style={{flexDirection: 'row'}}>
            <View style={{alignItems: 'flex-end'}}>
                <Text style={{fontSize: 13}}>{base}</Text>
            </View>
            <View style={{alignItems: 'flex-start'}}>
                <Text style={{fontSize: 10}}>{exponent}</Text>
            </View>
        </View>
    }

    render() {
        return(
            <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
                <Text>{(() => 'hello'+'world'.sub())()}</Text>
                {(() => this.sub('hello','world'))()}
                {(() => this.sub('2','6'))()}
            </View>
        );
    }
}

Result in iOS Simulator

5 Comments

Getting an error when I include this inside an existing View: "Views nested within a <Text> must have a width and height"
Also, from the React Native documentation, embedding a View within Text will work in iOS only. Not cross-platform.
That's right but if you are on android you could simply use textAlignVertical: 'top'
I think for now (if you want to use Text + nested exponents) you have to do really ugly mixtures of Views and Text Elements. Maybe take a look at productpains there is a voting system for future features. I added the link to CSS vertical align property page with a reference to the sub/super properties.
On Android, I tried the textAlignVertical: 'top' property like so: <Text>Productname<Text style={{fontSize: 9, textAlignVertical: 'top'}}>®</Text></Text> to no avail. Any thoughts?
1

The best method I found for this problem is not ideal, but it works cross-platform. The character I needed to superscript is the ® which some fonts have superscripted by default. So, I simply included a similar font family with the superscripted ® and it worked like magic.

1 Comment

I think in many cases this is the right answer. We discovered that the issue was that we'd stripped out unused glyphs in our font when we installed it (of 1800 in Source Sans Pro, we were only using 200); but looking back, one of those we stripped was a superscripted ®.
1

Simple method for superscript Text in 2023

import React, { Text } from 'react-native'

type Props = {
  children: string
}

const SuperscriptText: React.FC<Props> = ({children}) => {
  return (
    <Text
      style={{
        fontSize: 8, // Should be smaller than the other text
        textAlignVertical: 'top',
      }}
    >
      {children}
    </Text>
  )
}

export default SuperscriptText

Working on Android & iOs:

<Text>Default Text</Text>
<SuperscriptText>Hi</SuperscriptText>
<Text>Continue here</Text>

Important note: Currently this does not work when nesting text elements. See here: https://github.com/facebook/react-native/issues/30375

Comments

0

Wrap your super text inside of a normal test and all the style verticalAlign: 'super', to the super text.

  <Text style={{fontSize: 16}}>
   Main Text and{' '}
    <Text style={{fontSize: 10, lineHeight: 12, verticalAlign: 'super'}}>Superscript</Text>
  </Text>

Comments

0

For my use case (which runs up against this bug where textAlignVertical doesn't work), the only thing I could get to work was to create an image of the text I wanted superscripted, with space under it, and put that inline within the <Text> block I was working within.

Worked alright, because it was the "registered mark" symbol, rather than something the user actually needs to read; but admittedly a much-less-than-desirable solution. One drawback is you can't prevent line-breaking between the superscript and the word it's attached to without adding a &nbsp;.

Comments

0

As others have already mentioned, this is still an open issue with React Native's Text component.

There is a workaround, though.

Assuming this is what you're trying to display. Here, "sup" is supposed to be a superscript. This does not currently work with React Native.

<Text style={{ lineHeight: 22 }}>
    Lorem ipsum
    <Text 
        style={{"fontSize": 10, "lineHeight": 22, "textAlignVertical": "top"}}
    >
        sup
    </Text>
</Text>

This is what that looks like:
enter image description here

The trick is to wrap the superscript text in a View and match the lineHeight of the parent Text and the nested superscript/subscript Text

<Text style={{ lineHeight: 22 }}>
    Lorem ipsum
    <View>
        <Text 
            style={{"fontSize": 10, "lineHeight": 22, "textAlignVertical": "top"}}
        >
            sup
        </Text>
    </View>
</Text>

And, here's what the fixed superscript looks like:
enter image description here

Hope that helps!

Comments

-1
<View style={styles.container}>
  <Text>This is a </Text>
  <Text style={styles.superscript}>superscript</Text>
  <Text> example.</Text>
</View>

const styles = StyleSheet.create({ container: { flexDirection: 'row', justifyContent: 'center', alignItems: 'center', }, superscript: { fontSize: 10, lineHeight: 10, alignSelf: 'baseline', }, });

This works perfectly, also provide independent styling ability

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.