├── .gitignore ├── LICENSE ├── README.md ├── lib └── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | .idea 6 | .idea/* 7 | # Runtime data 8 | pids 9 | *.pid 10 | *.seed 11 | 12 | # Directory for instrumented libs generated by jscoverage/JSCover 13 | lib-cov 14 | 15 | # Coverage directory used by tools like istanbul 16 | coverage 17 | 18 | # nyc test coverage 19 | .nyc_output 20 | 21 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 22 | .grunt 23 | 24 | # node-waf configuration 25 | .lock-wscript 26 | 27 | # Compiled binary addons (http://nodejs.org/api/addons.html) 28 | build/Release 29 | 30 | # Dependency directories 31 | node_modules 32 | jspm_packages 33 | 34 | # Optional npm cache directory 35 | .npm 36 | 37 | # Optional REPL history 38 | .node_repl_history 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 unadlib 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-native-px2dp 2 | pixels convert to density-independent pixels. 3 | 4 | ### Default UI Size 5 | Default UI design size is based on 1280x720. 6 | 7 | ### Installation 8 | ```bash 9 | npm install react-native-px2dp --save 10 | ``` 11 | 12 | ### Usage 13 | ```javascript 14 | import px,{px2dp} from 'react-native-px2dp'; 15 | const style = { 16 | width: 720 * px,//UI height size is 1280. 17 | } 18 | const otherStyle = { 19 | width: 720 * px2dp(480),//UI height size is 960. 20 | } 21 | ``` -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Author : unadlib 3 | * Date : 2017/4/18 4 | * Time : 下午3:01 5 | * Project [ react-native-px2dp ] Coded on WebStorm. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | import {Dimensions} from 'react-native'; 11 | 12 | const defaultHeight = 640; 13 | const deviceHeightDP = Dimensions.get('window').height; 14 | const ratePX = deviceHeightDP / defaultHeight; 15 | 16 | export function px2dp(height = defaultHeight) { 17 | return deviceHeightDP/height; 18 | } 19 | 20 | export default ratePX; 21 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-px2dp", 3 | "version": "1.1.0", 4 | "description": "pixels convert to density-independent pixels.", 5 | "main": "lib/index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/unadlib/react-native-px2dp.git" 12 | }, 13 | "keywords": [ 14 | "react-native", 15 | "px2dp", 16 | "react-native-px2dp" 17 | ], 18 | "author": "unadlib", 19 | "license": "MIT", 20 | "bugs": { 21 | "url": "https://github.com/unadlib/react-native-px2dp/issues" 22 | }, 23 | "homepage": "https://github.com/unadlib/react-native-px2dp#readme" 24 | } 25 | --------------------------------------------------------------------------------