There are two most famous tags available in HTML known as <sup> superscript and <sub> subscript. Superscript tag sets the half of character above on the text writing line like (A2+B2) and same as but in different manner the Subscript tag is opposite of superscript, In subscript tag the next character would be half down to the previous one like (H2SO4). So sometimes the developer needs to show mathematical equations and chemistry equations in our react native application, In react native using the lineHeight attribute of style we can actually achieve this type of functionality. So in this tutorial we would going to show SuperScript SubScript Text in Android iOS React Native Application Example Tutorial.
Contents in this project Show SuperScript SubScript Text in Android iOS React Native App Example Tutorial:
1. Import StyleSheet, Platform, View and Text component in your react native project.
1
2
3
|
import React, { Component } from ‘react’;
import { StyleSheet, Platform, View, Text } from ‘react-native’;
|
2. Create a Root View in render’s return block, This View is our main container view.
1
2
3
4
5
6
7
8
9
10
11
12
|
render() {
return (
<View style={styles.MainContainer}>
</View>
);
}
|
3. Create a secondary child view in root view. Inside this child view we would we would create text component like Superscript text using the lineHeight attribute.
As you can see in below code we would separate each text with separate Text component, This is necessary.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<View style={{flexDirection: ‘row’}}>
<Text style={{fontSize: 20, lineHeight: 30, color: ‘#000’}}>A</Text>
<Text style={{fontSize: 11, lineHeight: 18, color: ‘#000’}}>2</Text>
<Text style={{fontSize: 20, lineHeight: 30, color: ‘#000’}}>+</Text>
<Text style={{fontSize: 20, lineHeight: 30, color: ‘#000’}}>B</Text>
<Text style={{fontSize: 11, lineHeight: 18, color: ‘#000’}}>2</Text>
</View>
|
4. Creating another child view and inside this we would simply change the lineHeight attribute value and using it we would make the text look like Subscript text.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<View style={{flexDirection: ‘row’}}>
<Text style={{fontSize: 20, lineHeight: 30, color: ‘#000’}}>H</Text>
<Text style={{fontSize: 11, lineHeight: 37, color: ‘#000’}}>2</Text>
<Text style={{fontSize: 20, lineHeight: 30, color: ‘#000’}}>S</Text>
<Text style={{fontSize: 20, lineHeight: 30, color: ‘#000’}}>O</Text>
<Text style={{fontSize: 11, lineHeight: 37, color: ‘#000’}}>4</Text>
</View>
|
5. Creating Style.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
const styles = StyleSheet.create({
MainContainer :{
flex:1,
justifyContent: ‘center’,
alignItems: ‘center’,
paddingTop: (Platform.OS) === ‘ios’ ? 20 : 0,
margin: 10
}
});
|
6. Complete source code for App.js File :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
import React, { Component } from ‘react’;
import { StyleSheet, Platform, View, Text } from ‘react-native’;
export default class Project extends Component{
render() {
return (
<View style={styles.MainContainer}>
<View style={{flexDirection: ‘row’}}>
<Text style={{fontSize: 20, lineHeight: 30, color: ‘#000’}}>A</Text>
<Text style={{fontSize: 11, lineHeight: 18, color: ‘#000’}}>2</Text>
<Text style={{fontSize: 20, lineHeight: 30, color: ‘#000’}}>+</Text>
<Text style={{fontSize: 20, lineHeight: 30, color: ‘#000’}}>B</Text>
<Text style={{fontSize: 11, lineHeight: 18, color: ‘#000’}}>2</Text>
</View>
<View style={{flexDirection: ‘row’}}>
<Text style={{fontSize: 20, lineHeight: 30, color: ‘#000’}}>H</Text>
<Text style={{fontSize: 11, lineHeight: 37, color: ‘#000’}}>2</Text>
<Text style={{fontSize: 20, lineHeight: 30, color: ‘#000’}}>S</Text>
<Text style={{fontSize: 20, lineHeight: 30, color: ‘#000’}}>O</Text>
<Text style={{fontSize: 11, lineHeight: 37, color: ‘#000’}}>4</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
MainContainer :{
flex:1,
justifyContent: ‘center’,
alignItems: ‘center’,
paddingTop: (Platform.OS) === ‘ios’ ? 20 : 0,
margin: 10
}
});
|
Screenshot in Android device:
Screenshot in iOS device: