The Text component dose supports run time Height and Width updating feature using States. So in this tutorial we would going to create a react native app with set Change Text Component Height Width Dynamically on button click in react native android iOS application using Sates. We would simply call a function on button click and update the State values and according to them the height and width should be updated programmatically.
Contents in this example Change Text Component Height Width Dynamically on button click in React Native :
1. Import StyleSheet, View, Button and Text component in your project.
1
2
3
|
import React, { Component } from ‘react’;
import { StyleSheet, View, Button, Text} from ‘react-native’;
|
2. Create constructor() in your project. Now we would make 2 States named as Text_Height and Text_Width and set the default height and width is 100 * 100.
1
2
3
4
5
6
7
8
9
10
11
12
|
constructor(){
super();
this.state={
Text_Height : 100,
Text_Width : 100
}
}
|
3. Create a function named as Change_Height_Width(). Inside this function we would simply update the States value to 120 * 200. We would call this function on button click event.
1
2
3
4
5
6
7
8
9
10
|
Change_Height_Width=()=>{
this.setState({
Text_Height : 120,
Text_Width : 200
})
}
|
4. Create a Root View in render’s return block.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
render() {
return (
<View style={styles.MainContainer}>
</View>
);
}
|
5. Create a Text component in Root View. Now using the Style Array method we would call custom style class and its attribute dynamically. The width and height here is coming form States.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
render() {
return (
<View style={styles.MainContainer}>
<Text style={ [ styles.TextStyle, { height: this.state.Text_Height, width: this.state.Text_Width } ] }>
Hello React Native Developers.
Hello React Native Developers.
Hello React Native Developers.
Hello React Native Developers.
</Text>
</View>
);
}
|
6. Create a Button component in Root view and call the Change_Height_Width() function on button onPress={} event.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
render() {
return (
<View style={styles.MainContainer}>
<Text style={ [ styles.TextStyle, { height: this.state.Text_Height, width: this.state.Text_Width } ] }>
Hello React Native Developers.
Hello React Native Developers.
Hello React Native Developers.
Hello React Native Developers.
</Text>
<Button title=‘Change Text Component Height Width Dynamically on Button Click’ onPress={this.Change_Height_Width} />
</View>
);
}
|
7. Create Styles.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
const styles = StyleSheet.create({
MainContainer :{
flex:1,
justifyContent: ‘center’,
alignItems: ‘center’,
padding: 10,
},
TextStyle:{
textAlign:‘center’,
color: ‘#4CAF50’,
marginBottom: 10,
borderWidth: 1,
borderColor: ‘#009688’
}
});
|
8. 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
import React, { Component } from ‘react’;
import { StyleSheet, View, Button, Text} from ‘react-native’;
export default class Mynewproject extends Component {
constructor(){
super();
this.state={
Text_Height : 100,
Text_Width : 100
}
}
Change_Height_Width=()=>{
this.setState({
Text_Height : 120,
Text_Width : 200
})
}
render() {
return (
<View style={styles.MainContainer}>
<Text style={ [ styles.TextStyle, { height: this.state.Text_Height, width: this.state.Text_Width } ] }>
Hello React Native Developers.
Hello React Native Developers.
Hello React Native Developers.
Hello React Native Developers.
</Text>
<Button title=‘Change Text Component Height Width Dynamically on Button Click’ onPress={this.Change_Height_Width} />
</View>
);
}
}
const styles = StyleSheet.create({
MainContainer :{
flex:1,
justifyContent: ‘center’,
alignItems: ‘center’,
padding: 10,
},
TextStyle:{
textAlign:‘center’,
color: ‘#4CAF50’,
marginBottom: 10,
borderWidth: 1,
borderColor: ‘#009688’
}
});
|
Screenshot in Android device :