I would like to ask how react native handle or do the responsive font. For example in iphone 4s i Have fontSize: 14, while in iphone 6 I have fontSize: 18.
I managed to overcome this by doing the following.
Pick the font size you like for the current view you have (Make sure it looks good for the current device you are using in the simulator).
import { Dimensions } from 'react-native'
and define the width
outside of the component like so: let width = Dimensions.get('window').width;
Now console.log(width) and write it down. If your good looking font size is 15 and your width is 360 for example, then take 360 and divide by 15 ( = 24). This is going to be the important value that is going to adjust to different sizes.
Use this number in your styles object like so: textFontSize: { fontSize = width / 24 },...
Now you have a responsive fontSize.
I simply use the ratio of the screen size, which works fine for me.
const { width, height } = Dimensions.get('window');
// Use iPhone6 as base size which is 375 x 667
const baseWidth = 375;
const baseHeight = 667;
const scaleWidth = width / baseWidth;
const scaleHeight = height / baseHeight;
const scale = Math.min(scaleWidth, scaleHeight);
export const scaledSize =
(size) => Math.ceil((size * scale));
Test
const size = {
small: scaledSize(25),
oneThird: scaledSize(125),
fullScreen: scaledSize(375),
};
console.log(size);
// iPhone 5s
{small: 22, oneThird: 107, fullScreen: 320}
// iPhone 6s
{small: 25, oneThird: 125, fullScreen: 375}
// iPhone 6s Plus
{small: 28, oneThird: 138, fullScreen: 414}
Because responsive units aren't available in react-native at the moment, I would say your best bet would be to detect the screen size and then use that to infer the device type and set the fontSize conditionally.
You could write a module like:
function fontSizer (screenWidth) {
if(screenWidth > 400){
return 18;
}else if(screenWidth > 250){
return 14;
}else {
return 12;
}
}
You'll just need to look up what the default width and height are for each device. If width and height are flipped when the device changes orientation you might be able to use aspect ratio instead or just figure out the lesser of the two dimensions to figure out width.
This module or this one can help you find device dimensions or device type.
We can use flex layout and use adjustsFontSizeToFit={true} for responsive font sizes.And the text would adjust according to the size of the container.
<Text
adjustsFontSizeToFit
style={styles.valueField}>{value}
</Text>
But in styles you need to put a fontsize as well only then will adjustsFontSizeToFit work.
valueField: {
flex: 3,
fontSize: 48,
marginBottom: 5,
color: '#00A398',
},
I recently ran into this problem and ended up using react-native-extended-stylesheet
You can set you rem
value and additional size conditions based on screen size. As per the docs:
// component
const styles = EStyleSheet.create({
text: {
fontSize: '1.5rem',
marginHorizontal: '2rem'
}
});
// app entry
let {height, width} = Dimensions.get('window');
EStyleSheet.build({
$rem: width > 340 ? 18 : 16
});
Why not using PixelRatio.getPixelSizeForLayoutSize(/* size in dp */);
, it's just the same as pd
units in Android.