Repository navigation
adjustsFontSizeToFit not working #20906
Copy link
Copy link
Closed
Labels
BugStaleThere has been a lack of activity on this issue and it may be closed soon.There has been a lack of activity on this issue and it may be closed soon.
Description
- 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,
},
});
Reacted by GetSource1234, Bruno Eduardo D'Angelo de Oliveira, Takeo Asai, Pablo de la Gándara, Spencer Kline, Jules Sam. Randolph, taifok, peteroid, Isaac Lem, Cristi and 3 more
Metadata
Metadata
Assignees
Labels
BugStaleThere has been a lack of activity on this issue and it may be closed soon.There has been a lack of activity on this issue and it may be closed soon.
Activity
pabloga265 commentedon Oct 22, 2018
This is still happening in 0.56 and 0.57, adjustsFontSizeToFit works fine on iOS but does nothing on Android
otaviogaiao commentedon Nov 7, 2018
Yep, still happening on 0.57. Is there any workaround?
pabloga265 commentedon Nov 7, 2018
Extremely hacky and extremely no-no. Full combat solution until I find something better
I created a text component that for Ios basically returns Text and in android returns a Text with this onLayout thing on top of it.
Please I can't encourage anyone enough to not use this in their projects, and find a real solution but if you are in a hurry this patched my problem
This is not a fix of course 1.7 is a random number that seems to fit my project properly in all resolutions I have tested so far, onLayout fires after render so there is a bit of a visual glitch etc... etc...
I will try to find a decent fix if it doesn't get fixed eventually but for now, I'm going with this shameful hack.
AshtonKem commentedon Dec 1, 2018
I can confirm that this is still happening.
@pabloga265 I think the above might work better if you can use Dimensions to determine screen width and calculate the correct fontSize before the first render.
jklingen92 commentedon Feb 9, 2019
@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 / 5where
lengthis 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 in
myFontFamilyafter 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.
carlosantoro96 commentedon Apr 21, 2019
If you want to use a value of "numberOfLines" >= 1 you have to modify a little this component, because how it's written it will always calculate a fontSize that will fit in just 1 line. I needed a numberOfLines = 2.
changing
const { adjustsFontSizeToFit, style, children } = this.propsin
const { adjustsFontSizeToFit, style, children,numberOfLines } = this.propsand
fontSize: Math.min(style.fontSize, Math.floor(5 * style.width / width))in
fontSize: Math.min(style.fontSize, Math.floor(5 * style.width / width * numberOfLines))did the trick for me.
stale commentedon Aug 4, 2019
Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.
stale commentedon Aug 11, 2019
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.