Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native max value for font scaling

I'm aware that I can disable font scaling globally in my React Native app by putting Text.defaultProps.allowFontScaling = false in the constructor of the root component (i.e. in the root index.js or App.js file). However, is there a way to allow a max value for scaling, instead of disabling it completely?

I've tried adjustsFontSizeToFit = true thinking it would prevent text from scaling up to really large sizes (which it does), but now all the text becomes too small and it breaks the styling of the app.

I've tried using minimumFontScale in combination with adjustsFontSizeToFit above, since according to the docs, minimumFontScale "specifies smallest possible scale a font can reach when adjustsFontSizeToFit is enabled." I set this value to 1.0 but the text still scales up to a ridiculously unreadable size (for example, if the "Larger Accessibility Size" is enabled in iOS and set to max scale) and setting it to a lower value (for example 0.25) allows the text to fit reasonably in my app, but the spacing and relative sizing becomes broken.

It would be nice if the text would remain it's default size, and still be allowed to scale up with "Accessibility Size" enabled in iOS, but only to a certain point–is there a way to achieve this?

like image 943
Attila Avatar asked Mar 26 '18 18:03

Attila


People also ask

What is default font size in React Native?

I've been testing in an iPhone 8 simulator running iOS 11, and through experimentation have come to the conclusion the default font displayed is 14 logical pixels (as in a brand-new element with no styling). However, checking the iOS style guidelines it would seem the default body text on iOS is intended to be 17pt.

How do I limit text length in React Native?

To set max length of the TextInput with React Native, we can set the maxLength prop. to set the maxLength prop to 10 to allow users to enter a max of 10 characters into the input.

How do I enlarge text in React Native?

You need to use fontSize attribute/property of stylesheet design, in order to set text font size in your Text component. Set Fontsize in react native application : Using below CSS properties you can set font size in your react native application.

Can I use PX in React Native?

From what I know, the javascript styling that we use in react js or react native uses pixels. Pixel ratio is only needed to support different size of mobile device screens.


2 Answers

You can use

allowFontScaling={false}

along with

Math.min(PixelRatio.getFontScale() * yourFontSizeBase, yourFontSizeBase)

to limit the max font size.

like image 110
James Avatar answered Oct 09 '22 10:10

James


In react native 0.58+ you can use the new prop maxFontSizeMultiplier inside the Text Component https://facebook.github.io/react-native/docs/text#maxfontsizemultiplier

like image 39
David Vittori Avatar answered Oct 09 '22 10:10

David Vittori