Skip to content

Instantly share code, notes, and snippets.

@gys-dev
Created June 25, 2022 15:01
Show Gist options
  • Save gys-dev/ff1c5771b1a140e393724205b36ee914 to your computer and use it in GitHub Desktop.
Save gys-dev/ff1c5771b1a140e393724205b36ee914 to your computer and use it in GitHub Desktop.
Resposive
import { Dimensions, PixelRatio } from 'react-native'
const guidelineBaseWidth = 390
const guidelineBaseHeight = 844
const { height, width } = Dimensions.get('window')
/**
*
* @param size: fontSize of text
* @returns re-calculate fontSize of text
*/
export function normalize(fontSize: number): number {
const scale = width / guidelineBaseWidth
const newSize = fontSize * scale
return Math.round(PixelRatio.roundToNearestPixel(newSize))
}
/**
*
* @param size: width of element
* @returns re-calculate width of element
*/
export function scale(size: number): number {
return (width / guidelineBaseWidth) * size
}
/**
*
* @param size: height of element
* @returns re-calculate height of element
*/
export function verticalScale(size: number): number {
return (height / guidelineBaseHeight) * size
}
export function moderateScale(size: number, factor: number = 0.5): number {
return size + (scale(size) - size) * factor
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment