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

后端 未结 6 2115
死守一世寂寞
死守一世寂寞 2020-12-16 10:14

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 decrea

6条回答
  •  悲哀的现实
    2020-12-16 10:42

    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 (
        
           {
              var { x, y, width, height } = event.nativeEvent.layout;
              setViewHeight(height)
            }}
          >
             {
                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.
            
          
    
          
            
              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.
            
          
        
      );
    }
    
    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.

提交回复
热议问题