ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Adjusting the length of a text on a box

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Guillaume Franck
Adjusting the length of a text on a box
on Jul 29, 2020 at 9:58:27 am

Hi all,
I'm trying to adjust the length of a text (number of characters) on a rectangle in background.
The idea is to cut the characters that stick out the rectangle, I don't use a matte because I would like to have only full letters.

The code I have so far crashes AE every time I test it.
Do you have an idea if it's doable and how to fix it ?

Thanks !

t = text.sourceText;
r = thisComp.layer("Shape Layer 1").content("Rectangle 1").content("Rectangle Path 1").size[0]- position[0]; // width - position of the layer
w= sourceRectAtTime().width;

t = t.substr(0,t.length-1); //text minus last character

Return to posts index

Filip Vandueren
Re: Adjusting the length of a text on a box
on Jul 29, 2020 at 2:04:25 pm

Hi there,

the problem is that the sourceRect is not re-calculated within the expression.
In effect you'll always get the sourceRect as if it's the original text. This makes sense, because you would get in a feedback loop.

I can't think of a very efficient way to do this.

The best solution I have is to have two txt-layers:
one layer into which you type your string. (should be placed at time=0)
With this expression for sourceText:


That'll make every possible string-length visible. (though you can hide the layer)

A second text-layer would look at the sourceRect at different times and choose the longest one that's shorter than your max_width.

l = thisComp.layer("my text");
max_chars = text.sourceText.valueAtTime(Number.MAX_VALUE).length;

max_width = 600;
for (i=0; i<=max_chars; i++) {
if (w>max_width) {

The downside of this approach: only one text-style allowed across the entire layer.
If you would need different font-sizes, bolds italics etc. then a solution with text-animators would be possible.

Return to posts index

Guillaume Franck
Re: Adjusting the length of a text on a box
on Jul 29, 2020 at 2:55:53 pm

This works perfectly, thank you!

My text is in one style, so this is not an issue.

Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2020 All Rights Reserved