Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Getting different line width in same text box

  • Getting different line width in same text box

    Posted by Evelyn Zhang on June 23, 2017 at 8:41 pm

    I’m trying a .sourceRectAtTime().width expression where the box shape is responsive to the length of the text —

    x=thisComp.layer("Text on Screen").sourceRectAtTime().width;
    y=70*1.4;
    [x+100,y]

    This code only returns the width of the entire text box, so the bar width are all the same. Does anyone have any idea on how to get the width of each individual line? without having to start different text box for every line? Thanks!!

    Filip Vandueren replied 5 years, 9 months ago 8 Members · 11 Replies
  • 11 Replies
  • Dan Ebberts

    June 24, 2017 at 6:38 pm

    This is definitely a hack of sorts, and I don’t know how robust/reliable it is, but you could give it a try. You would put this expression in the source text of your text layer:


    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
    value

    For the rectangle for the first line of text, you’d get the width this way:

    myLine = 1;
    L = thisComp.layer(“text”);
    w = L.sourceRectAtTime(-10*(myLine-1)-5).width

    For the second line of text you’d change the first line of the expression to:

    myLine = 2;

    etc.

    Dan

  • Evelyn Zhang

    June 27, 2017 at 12:48 am

    You, sir, are a literal angel. This is exactly what I need. THANK YOU SOOOO MUCH.

  • Ben Katz

    May 17, 2018 at 5:34 am

    Hi, I’m trying to reproduce exactly that, but its not working, can you guys please explain a little more or give an example?

    Thanks
    Ben

  • Andrew Mcclure

    August 2, 2018 at 9:39 pm

    Hi Dan, thank you for this! Do you know if its possible for this to work without manually creating a line break?

  • Dan Ebberts

    August 2, 2018 at 10:24 pm

    I’m not sure what you mean. Are you talking about box text? If so, I can’t think of anything.

    Dan

  • Kalleheikki Kannisto

    August 3, 2018 at 7:35 am

    The only way you could achieve that, that I can think of, is having code that broke the paragraph text into lines first around a certain character count. The line breaks would be different from the automatic line breaks in paragraph text, but then you could utilize Dan’s code to create the individual box widths. There are some existing threads on the subject of automatically adding line breaks at the space character around a given character count.

    Kalleheikki Kannisto
    Senior Graphic Designer

  • Andrew Mcclure

    August 3, 2018 at 10:50 am

    Hopefully this gif explains it better-

    I’m trying to find a way for it to work without having to hit the enter key after each line

  • Andrew Mcclure

    August 3, 2018 at 10:54 am

    This could work thank you so much Kalleheikki!

  • Stefano Gradaschi

    June 11, 2020 at 1:34 pm

    Thanks everyone for posting their suggestions. I kind of put together multiple expressions to do this trick. It works 99% of the times, there might be an odd combination of words that breaks it.I must say I am not an expert in coding so there might be dodgy bits here an there.Hope this will help anyone!;)Here is it how I did it:In the source text of the layer "text"a=comp("02.2_Subtitle Social").layer("text").text.sourceTexttxt = an = 61;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;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 valuein the size of the shape rectangle "myline-1'myLine = 1;L = thisComp.layer("text");w = L.sourceRectAtTime(-10*(myLine-1)-5).widthx = L.sourceRectAtTime(-10*(myLine-2)-5).widthtemp = content("Rectangle 1").content("Rectangle Path 1").size[0];[w+40,80]in the size of the shape rectangle "myline-2'L = thisComp.layer("text").text.sourceText;w = L.length;temp = content("Rectangle 1").content("Rectangle Path 1").size[0];[w*27-thisComp.layer("myLine-1").content("Rectangle 1").content("Rectangle Path 1").size[0]-20,80]in the opacity of the shape rectangle "myline-2'd=thisComp.layer("text").text.sourceTextif (d.length > 64 && d.length < 999 ) 100 else 0;if it’s a cow it will moo

  • Kevin Tey

    November 8, 2020 at 12:55 am

    Hi Stefano,
    I’m getting some errors here and there. Do you have a project file for reference?
    Still a noob and a project file could help my understanding greatly.

    Thanks!

Page 1 of 2

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