Superscript text in a React Native book

I would like to draw text as superscript in React Native. How will this be done? Is there a way to use the Javascript sup()string method to return a string as a superscript inside an object <Text>?

+4
source share
4 answers

I got this working for me using the 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 a link to it in action https://repl.it/Haap/0

Hooray!

+6
source

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>
+3
source

The Javascripts function sub()will only surround the text with tags <sub></sub>, and they are recognized as text in RN. You will need to create your own function, for example:

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

0
source

The best method I found for this problem is not perfect, but it works cross-platform. The character I need for the add-in is the character, which by default has some fonts. So, I just turned on a similar superscript ยฎ font family and worked like magic.

0
source

Source: https://habr.com/ru/post/1651585/


All Articles