ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Dynamic Line Breaks and Measuring Individual Line Lengths

FAQ   •   VIEW ALL
 Dynamic Line Breaks and Measuring Individual Line Lengths on Nov 9, 2017 at 5:03:06 pm

Hello good people!

I'm currently trying to get point text to add a line break after a set number of characters - this much works fine, But then I want to solve for the length of each new line, for a shape layer to fill in behind individual lines of text.

While I have two different scripts created by the incredible Dan Ebberts, I can't seem to figure out how to combine them. It is possible, that it would only be able to measure the length of the original line before the dynamic line breaks are added through expressions though.

Alternately, if it broke the text source into multiple point text layers, that would achieve the same effect. Any ideas?

Here's what I'm working with:

```//Dynamic Line Break txt = value; n = 20; outStr = ""; newLine = "" splt = txt.split(" ") for (i = 0; i &lt; splt.length; i++){ if ((newLine + " " + splt[i]).length > n){ if (outStr != "") outStr += "\r"; outStr += newLine; newLine = splt[i]; }else{ if (newLine != "") newLine += " "; newLine += splt[i]; } } if (newLine != ""){ if (outStr != "") outStr += "\r"; outStr += newLine; } outStr; //Line Length Measurement if (time &lt; 0){ s = value.split("\r"); txt = "" for (i = 0; i &lt; s.length; i++){ if (time > -10*(i+1)){ txt = s[i]; break; } } txt; }else```

 Re: Dynamic Line Breaks and Measuring Individual Line Lengthson Aug 6, 2018 at 7:32:41 pm

Hey Dan, were you ever able to figure this out? Having the exact same issue with no luck!

 Re: Dynamic Line Breaks and Measuring Individual Line Lengthson Aug 7, 2018 at 6:45:39 amLast Edited By Kalleheikki Kannisto on Aug 7, 2018 at 6:53:20 am

The first issue I see is that the second part of the expression is retrieving the original value of the text layer rather than the result from the first part. So at least that needs to change. Instead of `s = value.split("\r");` it would need to be `s = outStr.split("\r");`

There might be more that is needed, but that's the first thing that jumped out at me.

Edit: Ok, there's much more that needs to be done... At no point are we retrieving the result of the second part, so there's a whole third part missing here.

Kalleheikki Kannisto
Senior Graphic Designer

 Re: Dynamic Line Breaks and Measuring Individual Line Lengthson Aug 7, 2018 at 7:02:51 am

Alright, now I remember what this was all about.

Here's the text expression, fixed for your purposes:

```//Dynamic Line Break txt = value; n = 20; outStr = ""; newLine = ""; splt = txt.split(" "); for (i = 0; i < splt.length; i++){ if ((newLine + " " + splt[i]).length > n){ if (outStr != "") outStr += "\r"; outStr += newLine; newLine = splt[i]; }else{ if (newLine != "") newLine += " "; newLine += splt[i]; } } if (newLine != ""){ if (outStr != "") outStr += "\r"; outStr += newLine; } outStr; //Line Length Measurement if (time < 0){ s = outStr.split("\r"); txt = ""; for (i = 0; i < s.length; i++){ if (time > -10*(i+1)){ txt = s[i]; break; } } txt; }else{outStr} ```

Then you will retrieve the result to the width value of another layer (shape layer rectangle, for instance) with

```myLine = 1; L = thisComp.layer("text"); w = L.sourceRectAtTime(-10*(myLine-1)-5).width; [w, content("Rectangle 1").content("Rectangle Path 1").size[1]]```

for the first line

and iterate the myLine value for each subsequent rectangle. You'll need as many rectangles as there are lines in the longest possible text you anticipate.

To get the rectangles to scale from the right spot, move their anchor points to the left edge (if your text is left aligned, that is.)

Kalleheikki Kannisto
Senior Graphic Designer

 Re: Dynamic Line Breaks and Measuring Individual Line Lengthson Aug 7, 2018 at 2:47:02 pm

This is huge. I had given up all hope of ever figuring it out!

Thanks so much! Now I really have to study this to try and understand the magic at work here.

Cheers!!!