ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Text Size Constrained By Width X

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Jamie Nguyen
Text Size Constrained By Width X
on Apr 24, 2019 at 12:38:04 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!


Return to posts index

Sem Brys
Re: Text Size Constrained By Width X
on Apr 24, 2019 at 7:31:15 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)


Return to posts index

Sem Brys
Re: Text Size Constrained By Width X
on Apr 24, 2019 at 7:32:53 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...


Return to posts index


Christian Geijer
Re: Text Size Constrained By Width X
on Apr 24, 2019 at 7:31:34 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]


Return to posts index

Jamie Nguyen
Re: Text Size Constrained By Width X
on Apr 24, 2019 at 6:01:25 pm

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


Return to posts index

Jamie Nguyen
Re: Text Size Constrained By Width X
on Jun 12, 2019 at 12:40:07 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?



Return to posts index


Kalleheikki Kannisto
Re: Text Size Constrained By Width X
on Jun 12, 2019 at 6:10:53 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


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2019 CreativeCOW.net All Rights Reserved
[TOP]