All files / helpers normalizeText.js

37.5% Statements 12/32
20% Branches 6/30
100% Functions 1/1
37.5% Lines 12/32
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89                      16x       16x   16x 16x 16x                     16x 22x   22x       22x     22x       22x                                                                                     16x  
// 
// Method to normalize size of fonts across devices
//
// Some code taken from https://jsfiddle.net/97ty7yjk/ &
// https://stackoverflow.com/questions/34837342/font-size-on-iphone-6s-plus
//
// author: @xiaoneng
// date: 14/10/2016
// version: 03
//
 
const React = require('react-native'); // eslint-disable-line no-undef
const {
  PixelRatio,
  Dimensions
} = React;
 
const pixelRatio = PixelRatio.get();
const deviceHeight = Dimensions.get('window').height;
const deviceWidth = Dimensions.get('window').width;
 
// -- Testing Only --
// const fontScale = PixelRatio.getFontScale();
// const layoutSize = PixelRatio.getPixelSizeForLayoutSize(14);
// console.log('normalizeText getPR ->', pixelRatio);
// console.log('normalizeText getFS ->', fontScale);
// console.log('normalizeText getDH ->', deviceHeight);
// console.log('normalizeText getDW ->', deviceWidth);
// console.log('normalizeText getPSFLS ->', layoutSize);
 
const normalize = (size) => {
  Eif (pixelRatio === 2) {
    // iphone 5s and older Androids
    Iif (deviceWidth < 360) {
      return size * 0.95;
    } 
    // iphone 5
    Iif (deviceHeight < 667) {
      return size;
    // iphone 6-6s
    } else Iif (deviceHeight >= 667 && deviceHeight <= 735) {
      return size * 1.15;
    }
    // older phablets
    return size * 1.25;
  } 
  if (pixelRatio === 3) {
    // catch Android font scaling on small machines
    // where pixel ratio / font scale ratio => 3:3
    if (deviceWidth <= 360) {
        return size;
    }    
    // Catch other weird android width sizings
    if (deviceHeight < 667) {
      return size * 1.15;
    // catch in-between size Androids and scale font up
    // a tad but not too much
    }
    if (deviceHeight >= 667 && deviceHeight <= 735) {
      return size * 1.2;
    }
    // catch larger devices
    // ie iphone 6s plus / 7 plus / mi note 等等
    return size * 1.27;
  }
  if (pixelRatio === 3.5) {
    // catch Android font scaling on small machines
    // where pixel ratio / font scale ratio => 3:3
    if (deviceWidth <= 360) {
        return size;
    // Catch other smaller android height sizings
    }
    if (deviceHeight < 667) {
      return size * 1.20;
    // catch in-between size Androids and scale font up
    // a tad but not too much
    }
    if(deviceHeight >= 667 && deviceHeight <= 735) {
      return size * 1.25;
    }
    // catch larger phablet devices
    return size * 1.40;
  }
  // if older device ie pixelRatio !== 2 || 3 || 3.5
  return size;
};
 
module.exports = normalize; // eslint-disable-line no-undef