Short Guide to Font Scaling & Styling Fixes in React Native

February 08, 2020

3 min read

Short Guide to Font Scaling & Styling Fixes in React Native

React Native, the most widely used framework for building cross-platform applications which combines the best parts of native development with React, a JavaScript library for making user interfaces.

So, with the cross-platform iOS & Android support, there comes a wide variety of devices having different resolutions. The user interface has to be designed in such a way that the application looks somewhat similar throughout the range of devices.

While developing a React Native app, I came across multiple issues for this particular scenario:

  • Fonts looks really small & unreadable for bigger devices.
    - Solution: Font Scaling.
  • When device fonts has been scaled by system settings to the extent which breaks the UI of whole app.
    - Solution: Restrict Device Font Scaling.

Font Scaling

For making the UI look according to the size of device, we have to scale the font accordingly. So we use the device height, width & pixel ratio to calculate the font size to be used.

Below are the functions that can be used as per the needs to scale the fonts:

1import { Platform, PixelRatio, Dimensions } from 'react-native';
2import ExtraDimensions from 'react-native-extra-dimensions-android'
3import DeviceInfo from 'react-native-device-info';
4import { isIphoneX } from 'react-native-iphone-x-helper'
5const { width, height } = Dimensions.get("window");
6export default class DeviceUiInfo {
7 static platform = Platform.OS; //gives the device platform iOS or Android
8 static screenSize = { width, height }; //gives the width & height of device
9 static screenSizeWithPixelRatio = { width: width * PixelRatio.get(), height: height * PixelRatio.get() }; //calculate the width & height based on device pixel ratio
10 static guidelineBaseWidth = 350; //standard width which will be used as base for calculating the scale.
11 static guidelineBaseHeight = 680; //standard height which will be used as base for calculating the scale.
12 static isIphoneX = isIphoneX() //check if device is iPhoneX
13 static isTablet = DeviceInfo.isTablet() //check if device is Tablet
14 static appVersion = DeviceInfo.getVersion(); //gives app version
15 static softBarHeight = ExtraDimensions.get('SOFT_MENU_BAR_HEIGHT'); //gives soft menu bar height
16 static statusBarHeight = ExtraDimensions.get('STATUS_BAR_HEIGHT'); //gives status bar height
17 static fontScale = PixelRatio.getFontScale() //gives font scale based on pixel ratio
18
19 static getPlatform() {
20 return this.platform
21 }
22 static getScreenSize() {
23 return this.screenSize
24 }
25 static getScreenSizeWithPixelRatio() {
26 return this.screenSizeWithPixelRatio
27 }
28 static isIphoneX() {
29 return this.isIphoneX
30 }
31 static scale(size) {
32 return this.screenSize.width / this.guidelineBaseWidth * size;
33 }
34 static verticalScale(size) {
35 return (this.screenSize.height) / this.guidelineBaseHeight * size;
36 }
37 static moderateScale(size, factor = 0.5) {
38 return size + (this.scale(size) - size) * factor;
39 }
40 static actualScale(size) {
41 const inputSize = DeviceUiInfo.moderateScale(size)
42 return inputSize / this.fontScale
43 }
44}

Restrict Device Font Scaling

To solve the unnecessary Font Scaling set by the device settings, we have to override the font scaling.

Here, we are using NativeBase framework for UI components so we will be overriding it's Text component for example and also the Text component which is provided by react-native.

1if (Text.defaultProps == null) Text.defaultProps = {};
2if (NBText.defaultProps == null) NBText.defaultProps = {};
3Text.defaultProps.allowFontScaling = false;
4NBText.defaultProps.allowFontScaling = false;
5TextInput.defaultProps.allowFontScaling = false

These are the few solutions for the issues we can handle. Open to suggestions for the same.

Explore the true potential of the business you represent with Logicwind's custom app and software solutions, which are designed to boost your success to new heights. Get started today to turn your ideas into reality!