adjustsFontSizeToFit not working
See original GitHub issue- Review the documentation: https://facebook.github.io/react-native
- Search for existing issues: https://github.com/facebook/react-native/issues
- Use the latest React Native release: https://github.com/facebook/react-native/releases
Environment
React Native Environment Info:
System:
OS: macOS High Sierra 10.13.6
CPU: x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
Memory: 184.31 MB / 16.00 GB
Shell: 5.3 - /bin/zsh
Binaries:
Node: 8.11.3 - /usr/local/bin/node
Yarn: 1.9.4 - /usr/local/bin/yarn
npm: 5.6.0 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 11.4, macOS 10.13, tvOS 11.4, watchOS 4.3
Android SDK:
Build Tools: 23.0.1, 26.0.1, 26.0.3, 27.0.3, 28.0.0
API Levels: 23, 25, 26, 27, 28
IDEs:
Android Studio: 3.1 AI-173.4819257
Xcode: 9.4.1/9F2000 - /usr/bin/xcodebuild
npmPackages:
react: 16.4.1 => 16.4.1
react-native: 0.56.0 => 0.56.0
npmGlobalPackages:
react-native-cli: 2.0.1
Description
Since 0.54.x, adjustsFontSizeToFit option in Text has not been working. It worked as expected on 0.53.x
Reproducible Demo
import React, {Component} from 'react';
import {StyleSheet, Text, View} from 'react-native';
export default class App extends Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.text} allowFontScaling>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
backgroundColor: '#F5FCFF',
},
text: {
fontSize: 30,
color: '#333333',
marginBottom: 5,
},
});
Issue Analytics
- State:
- Created 5 years ago
- Reactions:13
- Comments:8
Top Results From Across the Web
Are there any alternatives to adjustsFontSizeToFit for Android?
In my testing, setting fontSize does not prevent "adjustsFontSizeToFit" from working. However, the initial size chosen does seem to effect ...
Read more >Text adjustsFontSizeToFit bug - Expo Snack
Text adjustsFontSizeToFit bug. adjustsFontSizeToFit with emoji as first char. Open with Expo Go. Open in editor. Need Expo? Don't have the Expo Go?...
Read more >Example of adjustsFontSizeToFit on Text in React Native
The adjustsFontSizeToFit prop is used to scale down the Text font size automatically according to given parent component width and height. The ...
Read more >Text - React Native Archive
Not all fonts have a variant for each of the numeric values, ... Specifies smallest possible scale a font can reach when adjustsFontSizeToFit...
Read more >adjustsFontSizeToFit not working in android, React-native font ...
If the app that isn't working is an Instant App, try these troubleshooting steps instead. After each step, restart your phone to see...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@pabloga265 I took your code and took out the constants. TLDR: you’re code breaks for some font-families, mine should fix that. See below.
Basically, I took figured if we have to render the text twice, we might as well get some useful information, so I’m rendering the first pass at fontSize 5 (if we start out too small it gets less accurate because the letters are so small). I did some testing and found the following:
monospace width = length * fontSize * 3 / 5wherelengthis the number of characters in the text. We can also get the width at fontSize 5:monospace width at fontSize 5 = length * 5 * 3 / 5 = length * 3Now, we can calculate how wide our actual text would be inmyFontFamilyafter render by applying the ratio between these two monospace widths to the reported width at fontSize 5 from onLayout:We can use also use this equation to calculate an appropriate fontSize to get the desired width of the text element.
I was also running into some problems with multiple adjustments so I created a switch that would only allow it to be adjusted one time.
I also made sure to use the smaller of the two fontSize values, as your code adjusts no matter what, while the adjustsFontSizeToFit property only shrinks text that is clipping.
This is still happening in 0.56 and 0.57, adjustsFontSizeToFit works fine on iOS but does nothing on Android