This Gist contains a simple expression for auto scaling the size of text layers to fit a composition based on the size of their text content. This is similar to the behavior in programs like Keynote, where the font size automatically scales down as you type more content.
Set up your composition and add your text layer. You'll want to get a baseline font size for things, so enter a bit of text and size it to look the way you want that amount of text to look. This is more of an art than a science, so go with your ❤️ here. 💪
This is where the magic happens. Since you can't use expressions to modify font sizes, you'll need to adjust the size of your text using the scale property. If you alt
plusclick
the stopwatch for your text layer's Scale property, the expression text box will appear in the timeline. Paste in the expression below and modify it to suit your needs. If you want, you can even map the hardcoded values to controls in your composition for more flexibility while you find a good fit.
Paste this code in for your text layer's scale expression
// This lets us get the width of the textbox containing your content.
layerWidth = thisLayer.sourceRectAtTime(time).width;
// This lets us get the width of the current composition.
compWidth = thisComp.width;
// This allows you to specify a maximum width that text boxes should not exceed.
// In this example it's 90%, but you can set it to whatever you need or even map it to a control.
//
// Pro-tip: If you have other content in the same horizontal space as your text, remember to
// account for that content when setting your maximumWidth.
maximumWidth = compWidth * .9;
// This calculates the current percentage of the maximum width that the text layer occupies.
percentageOfMaxWidth = layerWidth / maximumWidth * 100;
// We subtract that percentage from 100 to find out how much space we can spare.
// If the text box can grow a bit, leeway is positive. If it needs to shrink, leeway will be negative.
leeway = 100 - percentageOfMaxWidth;
// Finally, return an array of [width, height] scale.
// 100 is the baseline and leeway is how much the layer needs to grow or shrink to fit
[100 + leeway, 100 + leeway]
If you have stumbled upon this Gist, try out this expression and let me know how it works fro you. Good luck! 😃
The text gets really small if it's too long
Plus, if the text is small enought, there could be space for 2 rows using the original allocated area.
Thank you for the effort though!