前提:設(shè)計圖為iphone6尺寸(750*1334)况增,像素密度默認(rèn)為2
相關(guān)代碼:
import {
? ? ?Dimensions,
? ? ?PixelRatio,
} from 'react-native';
export const deviceWidth = Dimensions.get('window').width;? ? ? //設(shè)備的寬度
export const deviceHeight = Dimensions.get('window').height;? ? //設(shè)備的高度
let pixelRatio = PixelRatio.get();? ? ? //當(dāng)前設(shè)備的像素密度
const defaultPixel = 2;? ? ? ? ? ? ? ? ? ? ? ? ? //iphone6的像素密度
//px轉(zhuǎn)換成dp
const w2 = 750 / defaultPixel;
const h2 = 1334 / defaultPixel;
const scale = Math.min(deviceHeight / h2, deviceWidth / w2);? //獲取縮放比例
export function scaleSize(size: number) {
? ? ? ? ?size = Math.round(size * scale + 0.5);
? ? ? ? ?return size;
}
使用時:
fontSize:scaleSize(14)