Skip to content

Instantly share code, notes, and snippets.

@mmazzarolo
Last active December 23, 2019 14:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmazzarolo/daa05789c18378f8929f810a625fb1b7 to your computer and use it in GitHub Desktop.
Save mmazzarolo/daa05789c18378f8929f810a625fb1b7 to your computer and use it in GitHub Desktop.
React-Native metrics scaling
import { PixelRatio, Dimensions } from "react-native";
const isTabletLike = () => {
const pixelDensity = PixelRatio.get();
const adjustedWidth = Dimensions.get("screen").width * pixelDensity;
const adjustedHeight = Dimensions.get("screen").height * pixelDensity;
return (
(pixelDensity < 2 && (adjustedWidth >= 1000 || adjustedHeight >= 1000)) ||
(pixelDensity === 2 && (adjustedWidth >= 1920 || adjustedHeight >= 1920))
);
};
export const scale = (size: number) => {
const guidelineBaseWidth = isTabletLike() ? 520 : 350;
return (Dimensions.get("screen").width / guidelineBaseWidth) * size;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment