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

  • Filip Vandueren

    March 5, 2021 at 7:07 pm
  • Matt Welton

    September 17, 2021 at 1:07 am

    I’ve recently used this expression for a “textbox” .mogrt I’m making, but realized that if the inputted text has any return characters in it (i.e. manual line breaks) then the text in the subsequent paragraphs start getting line breaks in unusual places — as if the character count function gets thrown off somehow.

    There also appears to be a “space” character that’s placed in the very beginning of the text that doesn’t exist in the text itself.

    Does anyone have any ideas on how to remedy? The code is (unfortunately) a little outside of my ability, so I’m not sure how to troubleshoot it.

  • Matt Welton

    September 17, 2021 at 3:25 pm

    For context, here is the expression I’m using.

    txt = value;
    n = effect("Height & Width")("Slider");
    outStr = "";
    newLine = ""
    splt = txt.split(" ")
    //convertBreak = thisComp.layer("Body Content").text.sourceText.replace(/"\r"/g,"§");
    //convertBreak = thisComp.layer("Body Content").text.sourceText.split("\r").join("§");
    for (i = 0; i < splt.length; i++){
    if ((newLine + " " + splt[i]).length > n){
    if (outStr != "") outStr += "\n";
    outStr += newLine;
    newLine = splt[i];
    }else{
    newLine += " " + splt[i];
    }
    }
    if (newLine != ""){
    if (outStr != "") outStr += "\n";
    outStr += newLine;
    }
    outStr;
    //REconvertBreak = thisComp.layer("Body Content").text.sourceText.replace("§",/"\r"/g);
    //ReconvertBreak = thisComp.layer("Body Content").text.sourceText.split("§").join("\r");

    You’ll notice some code is commented out — these were my (failed) attempts to find/replace carriage returns with a symbol, then run the code that determines where line breaks should go, then restore the symbols to carriage returns again.

    I also tried replacing \r with \n in the original code to see if that’d resolve whatever conflict was occurring, but that had no effect either.

  • Matt Welton

    September 21, 2021 at 5:22 pm

    Dan, do you think what I’m trying to do above is possible?

  • Thomas Frenkel

    October 25, 2022 at 12:40 pm

    Hi Dan,

    is there a way to make this respect manually input line breaks? So that everytime I do a custom linebreak myself, i is reset to 0. That would be a dream.

    Thomas

  • Yevhen Chak

    November 6, 2022 at 1:25 pm

    Super script. If I transfer the text to a new line manually, then on the second line the script continues to count characters and the transfer to a new line will be done earlier. How to make the script work on the new magpie first? Dan, thank you so much

  • Pete Menich

    November 20, 2022 at 2:24 pm

    I’ve been trying something similar by hacking the original expression in this thread to get a subtitle to add a line break if the width of the text object goes above a certain length.

    This seems to be working…

    txt = thisComp.layer("Captions").text.sourceText;
    dist = thisComp.layer("Captions").sourceRectAtTime().width;
    Wguide = thisComp.layer("width guide").effect("Slider Control")("Slider")
    len = txt.length;
    if (dist > Wguide){
    n = len / 2;
    }else{
    n = len;
    }
    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;

    Except sometimes it splits the text over 3 lines when 2 will do – any suggestions?

  • Dan Ebberts

    November 20, 2022 at 7:02 pm

    Do you just need to split it into 2 pieces if it’s too long? If so, see if this works for you:

    txt = thisComp.layer("Captions").text.sourceText.value;
    dist = thisComp.layer("Captions").sourceRectAtTime().width;
    Wguide = thisComp.layer("width guide").effect("Slider Control")("Slider")
    len = txt.length;
    outStr = txt;
    if (dist > Wguide){
    if (len%2){
    i = Math.floor(len/2);
    j = i;
    }else{
    j = len/2;
    i = j - 1;
    }
    while (i >= 0 && j < len){
    if (txt[j] == " "){
    outStr = txt.substr(0,j) + "\r" + txt.substr(j+1);
    break;
    }
    if (txt[i] == " "){
    outStr = txt.substr(0,i) + "\r" + txt.substr(i+1);
    break;
    }
    i--;
    j++;
    }
    }
    outStr
  • Pete Menich

    November 21, 2022 at 10:39 am

    As always, amazing! Works like a charm.

    Is there a way that if after its been split over two lines and it’s still too long, that it could be split over 3, and then 4 lines etc etc

    I’ve also been trying to use this script….

    https://creativecow.net/forums/thread/paragraph-bouding-control/

    but as my text is being driven by marker comments, it doesn’t work.

    P

    Paragraph bouding control

  • Filip Vandueren

    November 21, 2022 at 1:39 pm

    The “bound paragraph” methos should still work if the text’s driven by markers.
    I think you’ll only need to change the first line of the sourceText parameter.

    But to be clear: that’s only if you absolutely need dynamic (moving) margins/width and reflowing.

    For subtitles, why not use a regular box-text and let that do the wrapping ?

Page 2 of 3

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