Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Expressions Breaking up text into even lines

  • Breaking up text into even lines

  • TIneke Van Schalkwyk

    August 29, 2020 at 3:56 am

    I would like my headlines to automatically break into multiple lines evenly instead of just wrapping the text and ending up with one word on a line.

    So far I have this script but I’m losing the last word of the text when I use it.
    Any ideas?

    function breakTextInEqualParts(myString, partNo) {
    var splits = [0];
    var subStrings = [];
    var strLength = myString.length;
    if (
    partNo < 2 ||
    myString == "" ||
    text.sourceText.indexOf("\n") != -1 ||
    text.sourceText.indexOf("\r") != -1 ||
    text.sourceText.length < 20
    )
    return myString;

    for (var i = 1; i < partNo; i++) {
    splits.push(Math.round(strLength / partNo) + splits[i - 1]);
    }
    splits.push(strLength);

    for (var i = 0; i < strLength - 1; i++) {
    subStrings.push(breakText(myString, splits[i], splits[i + 1]));
    }

    return subStrings.join("\n");

    function breakText(myString, idx1, idx2) {
    if (myString == "") return "";
    idx1 = calcIndex(myString, idx1);
    idx2 = calcIndex(myString, idx2);
    return myString.substring(idx1, idx2);

    function calcIndex(myString, idx) {
    if (idx == 0) return idx;
    var before = myString.lastIndexOf(" ", idx);
    var after = myString.indexOf(" ", idx + 1);

    if (before == -1 || (after != -1 && idx - before >= after - idx)) {
    idx = after;
    } else {
    idx = before;
    }
    return idx;
    }
    }
    }

    var len = text.sourceText.length;
    if (len > 40) parts = 2;
    else if (len > 20) parts = 3;
    else parts = 1;

    var ctrlStyle = thisComp.layer(index - 1).text.sourceText.style;
    val = breakTextInEqualParts(text.sourceText, parts);

  • Tomas Bumbulevičius

    August 29, 2020 at 10:52 am

    Hey Tlneke, could you please also share a phrase which gets lost? Just in case it relates to specific character count, testing this with exactly the same phrase would be the best. Cheers!

    Find out more:
    Motion Graphics Design & After Effects Tutorials
    On YT
    On VH

  • Filip Vandueren

    August 29, 2020 at 8:15 pm

    Dag Tineke, here’s my solution:


    t=text.sourceText.replace(/[\s|\u0003)]+/g, ' ').trim(); // remove all extraneous whitespace
    len=t.length;

    if (len > 40) parts = 2;
    else if (len > 20) parts = 3;
    else parts = 1;

    dll = (len/parts); // dll = desiredLineLength

    words = t.split(" ");

    do {
    output="";
    c=0;
    for (w of words) {
    c+=w.length;
    if (c > dll) { // new word would overflow
    if (c-dll < dll-c+w.length+1) {
    output+=w+"\n"; // word before overflow
    c=0;
    } else {
    output+="\n"+w+" "; // word after overflow
    c=w.length;
    }
    } else {
    c++;
    output+=w+" ";
    }
    }
    lines = (output.match(/\n/g)||[]).length +1;
    dll++;
    } while (lines > parts) // if long words tripped it up (too many lines), try again... dll is already increased just in case.

    output;

  • TIneke Van Schalkwyk

    September 15, 2020 at 2:23 am

    Thanks Fliip, this seems to do the trick!

    I’ve also been setting the style of the font using this expression:
    style.setFont(comp(“00 Color Control”).layer(“Headline Text Font Style”).text.sourceText);

    Just wondering how I can include this in the code provided?

  • Filip Vandueren

    September 15, 2020 at 8:56 am

    The last line of my expression (instead of just ‘output’) would become:

    style.setFont(comp(“00 Color Control”).layer(“Headline Text Font Style”).text.sourceText.style.font).setText(output);
  • Filip Vandueren

    September 15, 2020 at 9:02 am

    BTW, your example only copies the font-face from your reference layer, if you want to copy all of the styles (font, color, tracking, leading,…) from your reference at once:

    comp(“00 Color Control”).layer(“Headline Text Font Style”).text.sourceText.style.setText(output);

    You simply copy the whole style (wich also includes the text of the roginal) and then set a new Text (keeping all the styles)

Viewing 1 - 6 of 6 posts

Log in to reply.

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