question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement allowFontScaling on Android

See original GitHub issue

Android Text components are wanting the ability to disallow font scaling. This is a key feature for design in order to prevent breaking layouts when the user sets their font sizes to huge. Allowing font scaling by default is fine, but there should be a convenient way to disallow it when needed so that apps work well for all users. It is an accessibility concern.

I currently work around this applying the following function in my style sheets:

  // Returns a scale-invariant font size.
  // On iOS this is a no-op, since we can use `allowFontScaling` to disable
  // scaling there. On Android, this reduces the font size so that when Android scales it
  // it will give the desired font size.
  function fixedFont(size) {
    // NOTE: Font Scale should always be the same as the Pixel Ratio on iOS, making this
    // a no-op.
    return size * ReactNative.PixelRatio.get() / ReactNative.PixelRatio.getFontScale();
  }

I have a general idea of how to fix this, but my Java skills chops aren’t really up to the task of creating a pull request. The issue seems to be that in ReactTextShadowNode.java:390 we compute the font size from Android’s SP units, which scale. What we want, instead, is to do this from DIP units when allowFontScaling is false. This is the recommended way to handle non-scaling font sizes in Android.

So, we would want something like:

if (allowFontScaling) {
  fontSize = (float) Math.ceil(PixelUtil.toPixelFromSP(fontSize));
else {
  fontSize = (float) Math.ceil(PixelUtil.toPixelFromDIP(fontSize));
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:12
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

25reactions
mmckinley8commented, Feb 9, 2017

@sibelius if you setText.defaultProps.allowFontScaling = false in index.ios.js, it will set it globally.

2reactions
zweifischcommented, Nov 10, 2016

I’ve replace StyleSheet.create with the following function

export const createStyleSheet = dict => {
    for (let key of Object.keys(dict)) {
        for (let name of Object.keys(dict[key])) {
            if (name === 'fontSize' || name === 'lineHeight') {
                dict[key][name] = fixedFont(dict[key][name])
            }
        }
    }
    return StyleSheet.create(dict)
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is allowFontScaling=false in <Text/> component a bad practice?
i'm currently coding my new React Native app and i tried to use it on my brother's phone and i noticed the font...
Read more >
React Native allowFontScaling on Text Android iOS Example
The allowFontScaling prop is use to control whether fonts should scale to respect Text Size accessibility settings of our mobile phone.
Read more >
React Native Font Scaling & Styling Fixes - Our Blogs
For making the UI look according to the size of device we have to scale the font accordingly. So we use the device...
Read more >
Elements Library | React Navigation
Defaults to true on iOS and false on Android. labelStyle - Style object for the label. allowFontScaling - Whether label font should scale...
Read more >
Text - React Native
Both Android and iOS allow you to display formatted text by ... For React Native, we decided to use web paradigm for this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found