Skip to content

Instantly share code, notes, and snippets.

@yayanartha
Last active December 10, 2017 07:53
Show Gist options
  • Save yayanartha/e7d39c122ad246cd87b2b0feca7fe832 to your computer and use it in GitHub Desktop.
Save yayanartha/e7d39c122ad246cd87b2b0feca7fe832 to your computer and use it in GitHub Desktop.
A proper way to keep the 'ideal size' of components and fonts across screen dimensions.
import { Dimensions } from 'react-native';
// Guideline sizes are based on standard ~5" screen mobile device
const guidelineBaseWidth = 350;
const guidelineBaseHeight = 680;
const scale = (size, screen = Dimensions.get('window')) => {
return screen.width / guidelineBaseWidth * size;
};
const verticalScale = (size, screen = Dimensions.get('window')) => {
return screen.height / guidelineBaseHeight * size;
};
const moderateScale = (size, factor = 0.5) => {
return size + (scale(size) - size) * factor;
};
export { scale, verticalScale, moderateScale };
@barayuda
Copy link

barayuda commented Dec 3, 2017

Nice, thanks helpful mate (y)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment