Try this method.
I used the method onLayout to achieve this.
First, I wrapped my Text view inside a fixed size View, then I implement onLayout for both view, so that I can get the height of the fixed size View and the height of the Text view.
Next, if the Text view's required height is more than the height of our View, we decrease the font size of our Text view until the height can fit into our View.
Herein the test code, I made 2 Views, one in pink and one in yellow background, the font size of Text inside the pink View will be adjust to fit the View and the yellow Text will remain the same font size.
import React, { useState, useEffect } from 'react';
import { StyleSheet, Text, View } from 'react-native';
export default function App() {
const [size, setSize] = useState(20)
const [viewHeight, setViewHeight] = useState(0)
const [textHeight, setTextHeight] = useState(0)
useEffect(() => {
if (textHeight > viewHeight) {
setSize(size - 1) // <<< You may adjust value 1 to a smaller value so the text can be shrink more precisely
}
}, [textHeight])
return (
<View style={styles.container}>
<View
style={{
margin: 20,
backgroundColor: 'pink',
width: 200,
height: 200
}}
onLayout={(event) => {
var { x, y, width, height } = event.nativeEvent.layout;
setViewHeight(height)
}}
>
<Text
style={{
fontSize: size,
}}
onLayout={(event) => {
var { x, y, width, height } = event.nativeEvent.layout;
setTextHeight(height)
}}
>
Gemma is a middle grade novel that follows a curious explorer and her ring-tailed lemur, Milo,
as they hunt for the “most greatest treasure in the world”. Solving riddles, battling a bell-wearing
jaguar, and traveling the Eight Seas, Gemma’s adventures take her from a young girl to a brave captain,
whose only limits are the stars.
</Text>
</View>
<View
style={{
margin: 20,
backgroundColor: 'yellow',
width: 200,
height: 200
}}
>
<Text
style={{
fontSize: 20
}}
>
Gemma is a middle grade novel that follows a curious explorer and her ring-tailed lemur, Milo,
as they hunt for the “most greatest treasure in the world”. Solving riddles, battling a bell-wearing
jaguar, and traveling the Eight Seas, Gemma’s adventures take her from a young girl to a brave captain,
whose only limits are the stars.
</Text>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
You may notice the pink Text's font is having a shrinking animation when the font size is decreasing, I suggest that you can initial the Text opacity with 0 and after the font adjustment finish set it to 1 so user wont see the ugly animation.