React Native Responsive Font Size

前端 未结 14 1248
执念已碎
执念已碎 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条回答
  •  萌比男神i
    2020-11-29 17:12

    I simply use the ratio of the screen size, which works fine for me.

    const { width, height } = Dimensions.get('window');
    
    // Use iPhone6 as base size which is 375 x 667
    const baseWidth = 375;
    const baseHeight = 667;
    
    const scaleWidth = width / baseWidth;
    const scaleHeight = height / baseHeight;
    const scale = Math.min(scaleWidth, scaleHeight);
    
    export const scaledSize =
        (size) => Math.ceil((size * scale));
    

    Test

    const size = {
        small: scaledSize(25),
        oneThird: scaledSize(125),
        fullScreen: scaledSize(375),
    };
    
    console.log(size);
    
    // iPhone 5s
    {small: 22, oneThird: 107, fullScreen: 320}
    // iPhone 6s
    {small: 25, oneThird: 125, fullScreen: 375}
    // iPhone 6s Plus
    {small: 28, oneThird: 138, fullScreen: 414}
    

提交回复
热议问题