Skip to content

Instantly share code, notes, and snippets.

@loranallensmith
Last active April 12, 2021 09:30
Show Gist options
  • Save loranallensmith/f3c862e022945f13e5eb762eda8dfed3 to your computer and use it in GitHub Desktop.
Save loranallensmith/f3c862e022945f13e5eb762eda8dfed3 to your computer and use it in GitHub Desktop.
Auto Scaling Font Size in After Effects

Auto-scaling fonts in After Effects

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.

How to do it

Set things up

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. 💪

Make the scale property dynamic

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 altplusclick 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]

Try it out!

If you have stumbled upon this Gist, try out this expression and let me know how it works fro you. Good luck! 😃

@stichiboi
Copy link

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!

@cb109
Copy link

cb109 commented Feb 24, 2020

Thanks for taking the time to add the detailled explanations ❤️!

@lucasnegrao
Copy link

@stichiboi - you just have to create a text-area in order to get automatic rows

@stichiboi
Copy link

@lucasnegrao I tired it with the paragraph text, but the line is never created, since the text gets scaled down

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment