Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions I need text to break after a set number of characters

  • I need text to break after a set number of characters

    Posted by David Cabestany on March 11, 2015 at 3:08 pm

    I’m creating a series of titles, they all need to have the same background, so I’m duplicating a comp several times and then changing the text layer.

    Since I have to do this several times I thought of using thisComp.name on the text layer so I don’t have to go into each comp to change the text.

    The problem is the titles are fairly long, so I need the line to break after a few characters, for example:

    Data enriquecida
    de cada transacción

    as opposed to

    Data enriquecida de cada transacción

    I tried the expression pasted below, but although is not showing an error, it’s not working.
    Can anyone help me tweak it?

    Thanks in advance.
    D.

    txt=thisComp.name;
    i=0;
    if (txt.length % 16==0)
    {
    txt += "\r";
    i++;
    }
    txt;

    David Cabestany replied 11 years, 6 months ago 2 Members · 6 Replies
  • 6 Replies
  • Dan Ebberts

    March 11, 2015 at 4:41 pm

    More like this, I think:


    txt=thisComp.name;
    maxChars = 16;
    str = txt.substr(0,maxChars);
    i = maxChars;
    while (i < txt.length){
    str += "\r" + txt.substr(i,maxChars);
    i += maxChars;
    }
    str
    Dan

  • David Cabestany

    March 11, 2015 at 7:43 pm

    Thanks a lot Dan,

    It works perfectly.
    I’m not even going to pretend I understand what’s going on here.

    My idea turned out to be not so great after all, as I still need to go into each comp and change the length of the string on each expression, so I wasn’t able to avoid having to click on each comp individually.

    Still, a great expression which I’ll study and try to understand, maybe one day I’ll get it.

    Best,
    D.

  • Dan Ebberts

    March 11, 2015 at 7:52 pm

    >I still need to go into each comp and change the length of the string on each expression

    So that it breaks on a word boundary?

    Dan

  • David Cabestany

    March 11, 2015 at 10:24 pm

    Yes. If not it will break at a random letter, well, not random, but obviously it won’t be the same character on each headline.

    Best,
    D.

  • Dan Ebberts

    March 12, 2015 at 1:05 am

    It gets a lot more complicated, but I think this should be close:


    txt = thisComp.name.split(" ");
    maxChars = 16;
    str = line = "";
    for (i = 0; i < txt.length; i++){
    if ((line + ((line.length > 0) ? " " : "") + txt[i]).length > maxChars){
    str += ((str != "") ? "\r" : "") + line;
    line = txt[i];
    }else{
    line += ((line != "") ? " " : "") + txt[i];
    }
    }
    if (line != ""){
    str += ((str != "") ? "\r" : "") + line;
    }
    str

    Dan

  • David Cabestany

    March 12, 2015 at 2:37 pm

    Once more, thanks a lot for taking the time to look into this. This is amazing, pure sorcery.
    I added an slider control so now it’s super easy to adjust the max characters.

    I’m very grateful Dan.

    Best,
    D.

    txt = thisComp.name.split(" ");
    maxChars = effect("maxChars")("Slider");
    str = line = "";
    for (i = 0; i &lt; txt.length; i++){
    if ((line + ((line.length > 0) ? " " : "") + txt[i]).length > maxChars){
    str += ((str != "") ? "\r" : "") + line;
    line = txt[i];
    }else{
    line += ((line != "") ? " " : "") + txt[i];
    }
    }
    if (line != ""){
    str += ((str != "") ? "\r" : "") + line;
    }
    str

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