React Native Responsive Font Size

前端 未结 14 1275
执念已碎
执念已碎 2020-11-29 16:39

I would like to ask how react native handle or do the responsive font. For example in iphone 4s i Have fontSize: 14, while in iphone 6 I have fontSize: 18.

14条回答
  •  心在旅途
    2020-11-29 17:11

    I managed to overcome this by doing the following.

    1. Pick the font size you like for the current view you have (Make sure it looks good for the current device you are using in the simulator).

    2. import { Dimensions } from 'react-native' and define the width outside of the component like so: let width = Dimensions.get('window').width;

    3. Now console.log(width) and write it down. If your good looking font size is 15 and your width is 360 for example, then take 360 and divide by 15 ( = 24). This is going to be the important value that is going to adjust to different sizes.

      Use this number in your styles object like so: textFontSize: { fontSize = width / 24 },...

    Now you have a responsive fontSize.

提交回复
热议问题