Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Expressions Scaling text to text box expression

  • Scaling text to text box expression

  • Dave Thompson

    August 2, 2016 at 7:18 pm

    Hi –
    I’ve been searching around for an expression that will scale text to fit within a paragraph text box. Basically, I’ve created a text box that will feed in text from outside of AE and I need the text to scale to fit within a designated space.


  • Kalleheikki Kannisto

    August 3, 2016 at 12:02 pm

    It’s a pretty convoluted matter.

    The attached project is the best I’ve managed to do thus far. It reflows the text (breaking between words) so that the text area becomes approximately the same shape as the rectangle and then scales the resulting text area to fit into the rectangle. As long as the type is all the same formatting, it works pretty good.

    There are two slider controls on the text layer:

    1. “margin” for how much margin there is between the edge of the text box and the text.

    2. “leading ratio” which needs to be adjusted if you change the typeface, leading or other character formats. It is used to tell the reflow expression what the average height to width ratio for one letter is, including the leading (line spacing). If you get too much empty space at the right or at the bottom, adjust this setting.

    Easy way to see how this works: Resize the rectangle from the bottom right corner. (Note: If you make the text box very thin and tall, the end of the text will be missing. To fix that, make the original text box taller with the type tool.)

  • Dave Thompson

    August 3, 2016 at 5:37 pm

    Genius, thanks Kalle! I appreciate your efforts on this. Exactly what I needed.

    Thanks again!
    Dave Thompson

  • Dave Thompson

    August 4, 2016 at 5:19 pm

    Hey Kalle –
    One more thing if you have a moment. I’ve now tried to create an expression that will pull text from outside of After Effects and space it evenly on a path. It only needs to be one line but the biggest problem is the variation in the length of the text that will be brought in dynamically. For instance, one line of text is country names so one could be “Chile” and another is “The United States of America”. If you look at the example in the dropbox link below, you’ll see what I’m talking about (where it says “Switzerland”). Similar to what you did before, the expression would need to scale the text and adjust the tracking between each letter. On the flip side, when a short name like “Chile” is brought in, the text needs to be centered and letter spacing close together so it’s not too spread apart.

    If you wouldn’t mind giving me your thoughts on this, I would appreciate it.

    Thanks again!

  • Kalleheikki Kannisto

    August 5, 2016 at 1:12 pm

    If you make the text centered to begin with, all you should need is to use a text animator for the tracking, the tracking value being based on the length of the text. For tracking something like


    Actual values depend on the longest and shortest word you have. With these values a two-letter word would have the tracking value of 80 and a ten-letter word the tracking value of 0. Adjust to taste.

    If need be, you could scale the layer with the same idea, based on text length. Make sure your anchor point is where you want the scaling to center around.

  • Dave Thompson

    August 5, 2016 at 9:08 pm

    Thanks again, Kalle!

    Apparently I need to take some programming classes. This stuff is insane beneficial. I gave it the old college try but am not left brained enough to get this to work. If you feel like looking at the file, it’s in the Dropbox below.

    What I’m getting currently is that when the text is too long, the characters start piling up on each other. If there is a way to keep the tracking the same whether big or small, that would be sweet. Basically, the text will need to scale down after it reaches the max limit on the path.

    I appreciate your time!

  • Kalleheikki Kannisto

    August 6, 2016 at 8:06 am

    Expressions are extremely useful for repetitive tasks. Scripting even more so.

    Okay, looking at this again, I’m thinking the Linear expression would give you more control.

    If you still want the text to have some tracking depending on number of characters, you can do this for tracking:
    linear(text.sourceText.length, 5, 20, 25, -3)

    This means that for any input values (text.sourceText.length) from 5 to 20 the output values (tracking) go from 25 to -3. So, if you have 5 letters of less, tracking is 25, if you have 20 letters or more, tracking is -3, and in-between values are linearly interpolated.

    Similarly, you can use this for layer scaling. You’ll have to make sure that the path can hold the longest text by having ample extra length at each end, since it will be scaled down. And put the anchor point in the right place as the layer scales around it.

    s=linear(text.sourceText.length, 5, 20, 100, 50);

    (See if you can figure out what happens there based on the first example. Note that scale is a two-dimensional property, that is why you need a two-dimensional array [s,s].)

    Kalleheikki Kannisto
    Senior Graphic Designer

  • Dave Thompson

    August 8, 2016 at 7:22 pm

    Awesome! Everything works great outside of a few things. The text doesn’t see the bounds of the path and so when I type too many characters, it drops down to a second line. The anchor point also changes when there are multiple lines of text but that won’t be an issue if it is restricted to one line. I’ve been trying to look up expression help but not seeing much out there or not searching the right terms.

    Take a look at what’s happening here (Version 2):

    Thanks again!

  • Kalleheikki Kannisto

    August 17, 2016 at 3:56 pm

    I would try and select the type size so that you longest text fits on one line.

    Kalleheikki Kannisto
    Senior Graphic Designer

  • ramondo robinson

    December 1, 2017 at 7:22 pm

    hey Guys,
    This script was a live saver in the past but tried to use this script in a recent project. I noticed that once a stroke is added to the text the point size & leading are not the same. I know this because I duplicated the “Text” layer and did this…the two layers react differently to text being added. I just want to know why does this happen and how could it be fixed? Thanks!

    txt = value;
    margin = effect("margin")("Slider");
    char_count = txt.length;
    lead_ratio = effect("leading ratio")("Slider");
    rect_height = thisComp.layer("Rect").height*thisComp.layer("Rect").scale[1]/100-2*margin;
    rect_width = thisComp.layer("Rect").width*thisComp.layer("Rect").scale[0]/100-2*margin;
    rect_ratio = rect_width/rect_height;
    char_per_line = Math.round(Math.sqrt(char_count*rect_ratio*lead_ratio));
    lines = Math.ceil(char_count/(char_per_line));
    done = 0;
    esc = 0;
    len =0;
    //row = Math.floor(thisComp.layer("Rect").sourceRectAtTime(time).width*thisComp.layer("Rect").scale[0]/1220);//divisor depends on type size
    row = char_per_line;
    while (done == 0){
    esc ++;
    if (txt.length >= len+1){
    for (i = len; i > len-row; i--) if (txt[i] == " ") break;
    if (i > len-row){
    txt=txt.substr(0,i) + "\r" + txt.substr(i+1,9999);
    len = i;
    if (esc>100){done=1}

Viewing 1 - 10 of 12 posts

Log in to reply.

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy