Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Dynamic Line Breaks and Measuring Individual Line Lengths

  • Dynamic Line Breaks and Measuring Individual Line Lengths

    Posted by Dan Wegendt on November 9, 2017 at 5:03 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 < 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 = value.split("\r");
    txt = ""
    for (i = 0; i < s.length; i++){
    if (time > -10*(i+1)){
    txt = s[i];
    break;
    }
    }
    txt;
    }else

    Andrew Mcclure replied 8 years ago 3 Members · 5 Replies
  • 5 Replies
  • Andrew Mcclure

    August 6, 2018 at 7:32 pm

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

  • Kalleheikki Kannisto

    August 7, 2018 at 6:45 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

  • Kalleheikki Kannisto

    August 7, 2018 at 7:02 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

  • Dan Wegendt

    August 7, 2018 at 2:47 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!!!

  • Andrew Mcclure

    August 7, 2018 at 8:45 pm

    Thank you so much Kalleheikki!! This is amazing, I’m very grateful for all your help!

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