-
Notifications
You must be signed in to change notification settings - Fork 24.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Text] Auto adjust font size to fit Text node width. #728
Comments
+1, this is one of those things that native does much better than web and is really well-suited to shadow-thread computation |
👍 would love to see this, @danscan can you try putting a PR together to add this in? |
👍 me too. |
@nicklockwood @sahrens - do you think this is desirable to include in core? any thoughts for an implementation strategy for this? |
I'd be in favour of it. Interesting to consider how it would interact with flexbox though - perhaps it would only work if the width and height are set explicitly? |
I'm very new to the JS side of things, but I'd assume that it would shrink rather than wrapping past a specified number of lines. |
@MattFoley yes, much like the native behavior on UILabel, the font size would decrease to fit the width. |
@nicklockwood - I think it would be ideal if it were to support |
Here's a proposal for the API: there is an optional prop called If
In these scenarios, the text is allowed to wrap and it shrinks until all of the text can fit in the available space (and again, we have some lower bound so text doesn't become illegible). And finally, |
We already have a numberOfLines property though… |
Or are you proposing that we use the existing numberOfLines property, but augment its behavior to include shrinking the text as well as truncating it, with the min size defaulting to current size (no shrinking) unless otherwise specified? |
@nicklockwood, exactly -- if the proposed |
+1 - definitely need this |
+1 this would be great |
If anyone is interested, I made a really simplistic version of this for now that just tests the width/height of text at various font sizes and chooses the best one: https://gist.github.com/brentvatne/a267d10eabd2d91a8d44 Use with caution ;) A real implementation of it would be very welcome here |
@brentvatne this is awesome! |
@danscan - feel free to clean that up / improve it and publish it to npm if you like! |
My goal is to get this implemented in the next few days on the ObjC side, and as a prop for Text, |
This is still a big missing piece of React Native. Would love to get #4026 moving along... |
Hi there! This issue is being closed because it has been inactive for a while. But don't worry, it will live on with ProductPains! Check out its new home: https://productpains.com/post/react-native/text-auto-adjust-font-size-to-fit-text-node-width ProductPains helps the community prioritize the most important issues thanks to its voting feature. Also, if this issue is a bug, please consider sending a pull request with a fix. |
^ +1, this would be great to have, not sure how else to cope with varying text length in Android. Lmk if I'm missing something |
It's been built on iOS, just needs an Android implementation added. |
responsiveFontSize from react-native-responsive-dimensions can automatically adjust fontSize based on the device's screen size. |
I would like to have also this, so I can define width: 50%, height: 50% and text can fill this on every screen.. |
Or can one envision a "willWrap" event trigger where changing the font size in the handler have the rendering be recalculated instead of wrapping ? |
I managed to overcome this by doing the following.
Now you have a responsive fontSize. |
@wmonecke this is great but I would imagine most were already using this. I think the best way to explain what a lot of the people here want is a % size of the text/views parent. So in my card we are displaying Cards (trading cards) but they are pulled from one component, sometimes there are 3 in a row, someones 2 sometimes 1 taking up whole screen. Could even be 4 or 5. The point is having a text size that is lets say 50% of the parent (overall card width) is more desirable than device width/height. By using dimensions the text size is the same even when you have 3 items in a row or 2 in a row, which provides inconsistent viewing. |
Finally i found this solution that cut the text if is more large than screen size <Text adjustsFontSizeToFit numberOfLines={1} .... |
how complicated is it to have adjustsFontSizeToFit work for Android too? |
React-native should really solve this out-of-the-box. |
I came close to implementing this with the following workaround. Of course this would be exact if you used monospace fonts. const letterToWidthRatio = 0.5476; // Approximate this by taking the width of some representative text samples
const text = "Some text to display";
const buffer = 5 // arbitrary amount to decrease font size, just in case
const fontSize = containerWidth / (text.length * letterToWidthRatio) - 5;
render() {
return (
<View style={{width: containerWidth}}>
<Text style={{fontSize}}>{text}</Text>
</View>
)
} |
@JulianKingman How did you approximate letterToWidthRatio? I require something of the sort with the problem I am currently trying to solve. |
@privateOmega compare the number of characters with the length, using onLayout const text = “some example string”
<Text onLayout={({nativEvent: {layout}}) => {condole.log(text.length / layout.width)}}>{text}</Text> Try a few different strings, and keep in mind punctuation will probably cause problems, so you may want to filter those out in your final font size calculation. |
What about this? I'm not sure that this is new functionality but the video is kinda new, so maybe this is worth to implement in React Native? |
Hi! I'm looking for how I can achieve behavior similar to UILabel's
adjustsFontSizeToFitWidth=true
, but haven't been able to find anything.Is this possible on a Text node?
The text was updated successfully, but these errors were encountered: