How I can make the font size of the text auto change inside a view in react native?
I have text with different lengths, some of which doesn't fit the view so decreased the font size. I have checked the docs and found this but it's for iOS only and I need it for Android.
And does it work with other components like button and touchableopacity?
To change font size of text field in React Material UI, we can set the InputProps and the InputLabelProps prop to set the font size of the input box and the input label respectively. to set InputProps and InputLabelProps to { style: { fontSize: 40 } } so that the input box and the label both have font size 40px.
A bit improved Jeffy Lee code worked for me
const AdjustLabel = ({ fontSize, text, style, numberOfLines }) => { const [currentFont, setCurrentFont] = useState(fontSize); return ( <Text numberOfLines={ numberOfLines } adjustsFontSizeToFit style={ [style, { fontSize: currentFont }] } onTextLayout={ (e) => { const { lines } = e.nativeEvent; if (lines.length > numberOfLines) { setCurrentFont(currentFont - 1); } } } > { text } </Text> ); };
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