React Native Responsive Font Size

前端 未结 14 1266
执念已碎
执念已碎 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

    We use a simple, straight-forward, scaling utils functions we wrote:

    import { Dimensions } from 'react-native';
    const { width, height } = Dimensions.get('window');
    
    //Guideline sizes are based on standard ~5" screen mobile device
    const guidelineBaseWidth = 350;
    const guidelineBaseHeight = 680;
    
    const scale = size => width / guidelineBaseWidth * size;
    const verticalScale = size => height / guidelineBaseHeight * size;
    const moderateScale = (size, factor = 0.5) => size + ( scale(size) - size ) * factor;
    
    export {scale, verticalScale, moderateScale};
    

    Saves you some time doing many ifs. You can read more about it on my blog post.


    Edit: I thought it might be helpful to extract these functions to their own npm package, I also included ScaledSheet in the package, which is an automatically scaled version of StyleSheet. You can find it here: react-native-size-matters.

提交回复
热议问题