-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreact-native-autofit-size.js
More file actions
33 lines (28 loc) · 930 Bytes
/
react-native-autofit-size.js
File metadata and controls
33 lines (28 loc) · 930 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { Dimensions,PixelRatio,Text as RNText } from "react-native" ;
const uiWidth=750;// 设计稿的宽度
const uiHeight=1334; // 设计稿的高度
const defaultPixel = 2;
const scaleWidth = uiWidth / defaultPixel;
const scaleHeight = uiHeight / defaultPixel;
const pixelRatio = PixelRatio.get();
export const styleUtils = {
get screenWidth(){
return Dimensions.get('window').width ;
},
get screenHeight(){
return Dimensions.get('window').height ;
},
get fontScale(){
return PixelRatio.getFontScale() ;
},
get scale(){
return Math.min(this.screenWidth / scaleWidth, this.screenHeight / scaleHeight);
} ,
getAutoSize(size){
return Math.round(size * this.scale + 0.5);
},
getTextSize(fontSize){
return (Math.round((fontSize * this.scale + 0.5) * defaultPixel / this.fontScale))/defaultPixel;
},
} ;
export default styleUtils ;