Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to adjust font size to fit view in React Native for Android?

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?

like image 343
Taha Avatar asked Dec 28 '17 04:12

Taha


People also ask

How do I change font size in react?

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.


1 Answers

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>    );  };
like image 55
Yurii H Avatar answered Sep 19 '22 19:09

Yurii H