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?
解决方案
A bit improved Jeffy Lee code worked for me
const AdjustLabel = ({
fontSize, text, style, numberOfLines
}) => {
const [currentFont, setCurrentFont] = useState(fontSize);
return (
numberOfLines={ numberOfLines }
adjustsFontSizeToFit
style={ [style, { fontSize: currentFont }] }
onTextLayout={ (e) => {
const { lines } = e.nativeEvent;
if (lines.length > numberOfLines) {
setCurrentFont(currentFont - 1);
}
} }
>
{ text }
);
};