Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Text Size Constrained By Width X

  • Text Size Constrained By Width X

    Posted by Jamie Nguyen on April 24, 2019 at 12:38 am

    Hello All,

    I’m working on a project where I need to create an editable mogrt. The text needs to be constrained to a certain width. For example, if the word is “bake” the text size will automatically adjust itself to be 500 pixels width. Or if the word is “cheesecake” the text will automatically adjust itself to be the same 500 pixels width. Is this something that can be done using expressions?

    Any help is greatly appreciated! Thank you!

    Kalleheikki Kannisto replied 6 years, 11 months ago 4 Members · 6 Replies
  • 6 Replies
  • Sem Brys

    April 24, 2019 at 7:31 am

    Something simple I came up with, add it in the scale parameter of the text layer:

    myWidth = sourceRectAtTime(time).width;
    myHeight = sourceRectAtTime(time).height;
    MaxWidth = 500;
    MaxHeight = 500;

    [100*(MaxWidth/myWidth),100*(MaxHeight/myHeight)];

    To explain: it reads the width and height of the text layer (sourcerect) then scales it by the ratio compared to the maximum width or height.
    You should connect the MaxWidth and MaxHeigth variables to a slider effect, then it would be something like that:

    MaxWidth = effect("MaxWidth")("Slider");

    If you want only 1 value, you can remove the MaxHeight and then it would be:

    [100*(MaxWidth/myWidth),100*(MaxWidth/myHeight)];

    Doesn’t seem to be 100% the pixel value, but maybe it’s good enough ☺ (if I find the time I will try to find out why it’s not 100% the given pixel value)

  • Christian Geijer

    April 24, 2019 at 7:31 am

    This is one way. Apply this to the scale property.

    L = thisLayer;
    w = L.sourceRectAtTime().width;

    maxW = 500;
    sca = (maxW/w) * 100;

    [sca, sca]

  • Sem Brys

    April 24, 2019 at 7:32 am

    Ok, so it is 100% the given value, I compared with a rectangle that was 500 pixels in width and height, but forgot to turn off the stroke…

  • Jamie Nguyen

    April 24, 2019 at 6:01 pm

    These are amazing thank you! I ended up using yours Christian, worked like a charm!

  • Jamie Nguyen

    June 12, 2019 at 12:40 am

    So I’ve been using what Christian came up with and it’s working great. But now instead of 1 line of text in my text layer I want 2 lines. When I add a second line, both lines are not scaled to be 540px but rather only 1 line is. Is there any way to get both lines of text to always scale to 540px?

  • Kalleheikki Kannisto

    June 12, 2019 at 6:10 am

    You need to split to two text boxes with the same expression. You will need to add some code to move one or both of the lines vertically based on the scale so as to adjust the space between the two lines as well.

    Kalleheikki Kannisto
    Senior Graphic Designer

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