Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Insert Line Break every X number of characters

  • Insert Line Break every X number of characters

    Posted by Jonathan Reyes on December 6, 2016 at 7:19 pm

    I found this great expression by Dan Ebberts and Bryce Poole in the following post which creates a line break after a certain amount of characters:
    https://forums.creativecow.net/thread/227/27641

    I was trying to modify it so it could continue to insert line breaks after X amount of characters instead of just the first time. For example, after every 20th character, insert a line break, essentially creating a bounding box via expressions with multiple lines of text each being 20 characters long. I know a little bit of expressions, but haven’t really messed with loops so I was wondering if anyone could help. Thanks!

    txt = value;
    if (thisComp.layer("Overlay TXT Here").text.sourceText.length >= 61){
    for (i = 61; i > 0; i--) if (txt[i] == " ") break;
    if (i > 0)
    txt.substr(0,i) + "\r" + txt.substr(i+1)
    else
    txt.substring(0,61) + "\r" + txt.substring(62,999);
    }else{
    txt
    }

    Pete Menich replied 1 year, 7 months ago 10 Members · 22 Replies
  • 22 Replies
  • Dan Ebberts

    December 6, 2016 at 7:49 pm

    Something like this should work:


    idx = 0;
    txt = value;
    outStr = "";
    n = 20; // line break every n characters
    while (idx < txt.length){
    if (idx > 0) outStr += "\r";
    outStr += txt.substr(idx,n);
    idx += n;
    }
    outStr

    Dan

  • Jonathan Reyes

    December 6, 2016 at 7:55 pm

    This is great, having this written out really helps me break down what’s going on here. Thanks Dan!

  • Jonathan Reyes

    December 6, 2016 at 8:18 pm

    Here’s something tricky…in the previous linked post, you found a way to skip the line break if it was in the middle of the word. I’ve been staring at the expressions for a while trying to combine to 2 to see if I can make it break before 20 characters if the 20th character is in the middle of a word. I think I may now be trying to punch above my weight, but is something like that possible?

  • Dan Ebberts

    December 6, 2016 at 8:30 pm

    Something like this maybe:


    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{
    newLine += " " + splt[i];
    }
    }
    if (newLine != ""){
    if (outStr != "") outStr += "\r";
    outStr += newLine;
    }
    outStr;

    Dan

  • Jonathan Reyes

    December 6, 2016 at 8:47 pm

    Genius. I’ll be honest, it’s gonna take a me a bit to go through and follow what’s happening, but overall it seems to work! It does however seem to be adding a space as the very first character of the text layer. I THINK it’s caused by the line that puts spaces between the split up words (newLine += ” ” + splt[i];) , but I will try and work around it. Thank you Dan for your incredibly fast and extremely useful response.

  • Dan Ebberts

    December 6, 2016 at 9:05 pm

    You’re right. Sorry about that. This should be better:


    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;

    Dan

  • Jonathan Reyes

    December 6, 2016 at 9:27 pm

    That did the trick, thanks Dan!

  • Avinash Ramanath

    December 21, 2016 at 11:18 am

    Hi Dan,
    While this is working great, is there a way to keep the text centralized to the comp.


  • Andrei Popa

    May 13, 2019 at 1:33 pm

    Apply this to the Anchor point

    var left = sourceRectAtTime(time,false).left;
    var top = sourceRectAtTime(time,false).top;
    var myWidth = sourceRectAtTime(time,false).width;
    var myHeight = sourceRectAtTime(time,false).height;
    var x = left + myWidth/2;
    var y = top + myHeight/2;
    [x,y]

    Andrei
    My Envato portfolio.

  • Myles Robinette

    March 5, 2021 at 3:34 pm

    Is there a way to update this expression so that the text starts a new line after every X amount of pixels rather than X amount of characters? Is it possible to measure the length of the text using .sourceRectAtTime().width and have the text start a new line after it reaches the desired width?

Page 1 of 3

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