Last active
March 21, 2024 07:51
-
-
Save neyron163/99538ee81d49135eeb80d70400d87e07 to your computer and use it in GitHub Desktop.
React native font scaling all devices, iphone, android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This code enables the adaptation of interface element sizes to different screen sizes and font scales, | |
// using units of measurement that are independent of pixel density. | |
// This ensures a more consistent and pleasant user experience when using the application. | |
import { Dimensions, PixelRatio } from 'react-native'; | |
// TARGET_WIDTH signifies the base screen size to which the application adapts. | |
// The standard screen width for development: 375 dp is often used as the standard screen width for developing interfaces, as it corresponds to the screen size of iPhone 6/7/8 Plus. | |
// This makes it a good base value for creating an adaptive design that will look good on most modern smartphones. | |
// PS - You don't need to worry about other iPhones; we've tested on 6s, 8, 10, 14 as well as on several Androids realme 11, gtneo2, samsung s23..., and everything works well. | |
const TARGET_WIDTH = 375; | |
export const { width: SCREEN_WIDTH, height: SCREEN_HEIGHT, fontScale } = Dimensions.get('window'); | |
// This code performs the conversion of size from "density-independent pixels" (dp) to pixels, taking into account the current screen sizes and font scale, to ensure the adaptability of the interface. | |
export const scaleSize = dp => | |
PixelRatio.roundToNearestPixel((dp * (SCREEN_WIDTH / TARGET_WIDTH)) / fontScale); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment