I'm trying to change Button font-size on my react native app, but I got an error. Does anyone know how to properly do it? Thank you.
To change a button's text on click in React:Track the text of the button in a state variable. Set the onClick prop on the button element. When the button gets clicked, update the state variable.
You need to use fontSize attribute/property of stylesheet design, in order to set text font size in your Text component. Set Fontsize in react native application : Using below CSS properties you can set font size in your react native application.
import { View, Button, StyleSheet, TouchableOpacity, Text } from "react-native"; To create custom buttons, you need to customize the <TouchableOpacity /> component and include the <Text /> component inside of it to display the button text.
You can use react-native-elements with titleStyle
props.
import {Input, Button} from 'react-native-elements';
<Button
onPress={this.addPicture}
titleStyle={{
color: "white",
fontSize: 16,
}}
buttonStyle={{
backgroundColor: "white",
borderRadius: 60,
flex: 1,
height: 30,
width: 30,
}}
title="+"
/>
I have a feeling you're not using a Text
element inside of your Touchable
:
import React from 'react';
import { TouchableWithoutFeedback, Text } from 'react-native';
export default function ComponentName() {
return (
<TouchableWithoutFeedback>
<Text style={{ fontSize: 24 }}>Button Text</Text>
</TouchableWithoutFeedback>
);
}
Unfortunately, according to the documentation (https://reactnative.dev/docs/button) you can't change a font-size of a button. The only style prop you can change is color
.
<Button
onPress={onPressLearnMore}
title="Learn More"
color="#841584"
accessibilityLabel="Learn more about this purple button"
/>
Here is my solution to easily style buttons using TouchableOpacity
with Text
:
import React, { Component } from 'react';
import { View, StyleSheet, TouchableOpacity, Text } from "react-native";
export default class CustomButton extends Component {
render(){
return (
<View style={styles.container}>
/* Custom Button */
<TouchableOpacity
style={styles.customBtnBG}
onPress={() => {}}
>
<Text style={styles.customBtnText}>Button</Text>
</TouchableOpacity>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center"
},
/* Here style the text of your button */
customBtnText: {
fontSize: 40,
fontWeight: '400',
color: "#fff",
},
/* Here style the background of your button */
customBtnBG: {
backgroundColor: "#007aff",
paddingHorizontal: 30,
paddingVertical: 5,
borderRadius: 30
}
});
Use this library https://github.com/APSL/react-native-button instead of Button component in react native.
<View>
<Button
style={{
backgroundColor: "#FE434C",
borderColor: "transparent",
borderRadius: 20,
width: 250
}}
textStyle={{ color: "#FFFFFF", fontSize: 20 }}
>
Hello
</Button>
</View>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With