Superscript Text in React Native

后端 未结 5 408
孤独总比滥情好
孤独总比滥情好 2020-12-06 02:56

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 r

相关标签:
5条回答
  • 2020-12-06 03:15

    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>
    
    0 讨论(0)
  • 2020-12-06 03:21

    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!

    0 讨论(0)
  • 2020-12-06 03:28

    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.

    0 讨论(0)
  • 2020-12-06 03:30

    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>
    
    0 讨论(0)
  • 2020-12-06 03:35

    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>
            );
        }
    }
    

    0 讨论(0)
提交回复
热议问题