forked from react-native-elements/react-native-elements
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request react-native-elements#68 from xiaoneng/master
Add normalize text helper function
- Loading branch information
Showing
10 changed files
with
116 additions
and
16 deletions.
There are no files selected for viewing
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 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 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 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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// | ||
// Method to normalize size of fonts across devices | ||
// | ||
// Some code taken from https://jsfiddle.net/97ty7yjk/ & | ||
// https://stackoverflow.com/questions/34837342/font-size-on-iphone-6s-plus | ||
// | ||
// author: @xiaoneng | ||
// date: 14/10/2016 | ||
// version: 03 | ||
// | ||
|
||
const React = require('react-native'); | ||
const { | ||
PixelRatio, | ||
Dimensions | ||
} = React; | ||
|
||
const pixelRatio = PixelRatio.get(); | ||
const deviceHeight = Dimensions.get('window').height; | ||
const deviceWidth = Dimensions.get('window').width; | ||
|
||
// -- Testing Only -- | ||
// const fontScale = PixelRatio.getFontScale(); | ||
// const layoutSize = PixelRatio.getPixelSizeForLayoutSize(14); | ||
// console.log('normalizeText getPR ->', pixelRatio); | ||
// console.log('normalizeText getFS ->', fontScale); | ||
// console.log('normalizeText getDH ->', deviceHeight); | ||
// console.log('normalizeText getDW ->', deviceWidth); | ||
// console.log('normalizeText getPSFLS ->', layoutSize); | ||
|
||
const normalize = (size) => { | ||
if (pixelRatio === 2) { | ||
// iphone 5s and older Androids | ||
if (deviceWidth < 360) { | ||
return size * 0.95; | ||
} | ||
// iphone 5 | ||
if (deviceHeight < 667) { | ||
return size; | ||
// iphone 6-6s | ||
} else if (deviceHeight >= 667 && deviceHeight <= 735) { | ||
return size * 1.15; | ||
} | ||
// older phablets | ||
return size * 1.25; | ||
} | ||
if (pixelRatio === 3) { | ||
// catch Android font scaling on small machines | ||
// where pixel ratio / font scale ratio => 3:3 | ||
if (deviceWidth <= 360) { | ||
return size; | ||
} | ||
// Catch other weird android width sizings | ||
if (deviceHeight < 667) { | ||
return size * 1.15; | ||
// catch in-between size Androids and scale font up | ||
// a tad but not too much | ||
} | ||
if (deviceHeight >= 667 && deviceHeight <= 735) { | ||
return size * 1.2; | ||
} | ||
// catch larger devices | ||
// ie iphone 6s plus / 7 plus / mi note 等等 | ||
return size * 1.27; | ||
} | ||
if (pixelRatio === 3.5) { | ||
// catch Android font scaling on small machines | ||
// where pixel ratio / font scale ratio => 3:3 | ||
if (deviceWidth <= 360) { | ||
return size; | ||
// Catch other smaller android height sizings | ||
} | ||
if (deviceHeight < 667) { | ||
return size * 1.20; | ||
// catch in-between size Androids and scale font up | ||
// a tad but not too much | ||
} | ||
if(deviceHeight >= 667 && deviceHeight <= 735) { | ||
return size * 1.25; | ||
} | ||
// catch larger phablet devices | ||
return size * 1.40; | ||
} | ||
// if older device ie pixelRatio !== 2 || 3 || 3.5 | ||
return size; | ||
} | ||
|
||
module.exports = normalize; |
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 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 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 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