Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Adding line breaks to a block of text based on character length

  • Adding line breaks to a block of text based on character length

    Posted by TIneke Van Schalkwyk on July 31, 2020 at 4:09 am

    I’m trying to automatically break headline text into 2 or 3 lines EVENLY.
    The parameters would be something like:
    If text length > 40 characters add line break to 1/2 way (not breaking words)
    If text length > 20 but < 40 characters split txt equally into 3 lines

    TIneke Van Schalkwyk replied 5 years, 9 months ago 4 Members · 8 Replies
  • 8 Replies
  • Brice Munn

    July 31, 2020 at 4:08 pm

    Would a text bounding box work? Just click and drag with the type tool to create a rectangular text box like you would make in word processing or graphics software. Then you can use the normal settings in the Character and Paragraph windows to control the typesetting.

  • TIneke Van Schalkwyk

    August 2, 2020 at 12:10 pm

    Hmm, no this wouldn’t work as it wouldn’t divide the text evenly over the lines. So you might be left with a single word on the last line.

    So far we have this which is working great for shorter texts where we only want two lines.
    But for longer text, we need it to divide into 3 lines.

    function breakInTwo(myString) {
    var mid = Math.floor(myString.length / 2);
    var before = myString.lastIndexOf(” “, mid);
    var after = myString.indexOf(” “, mid + 1);

    if (before == -1 || (after != -1 && mid – before >= after – mid)) {
    mid = after;
    } else {
    mid = before;
    }
    return myString.substring(0, mid) + “\n” + myString.substring(mid + 1);
    }

    if (text.sourceText.indexOf(“\n”) != -1 || text.sourceText.indexOf(“\r”) != -1 || text.sourceText.length<18) {
    value;
    } else {
    val = breakInTwo(text.sourceText);

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

  • Tomas Bumbulevičius

    August 4, 2020 at 11:22 am

    Hey Tineke, just wondering – lines still remains not perfectly equal in sizes, due to different letters width. Is that an issue or not at all for you?

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

  • TIneke Van Schalkwyk

    August 4, 2020 at 12:04 pm

    No that doesn’t matter to me.

  • Tomas Bumbulevičius

    August 5, 2020 at 11:42 am

    Tineke – then caps lock in the first post on ‘evenly’ does not really make sense.

    Reason – IF you have a superlongtextinoneline this won’t work nicely ! (:

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

  • Andrei Popa

    August 6, 2020 at 8:56 am

    You can try this. You can write as many ifs as you want to set the part number you want your text split into.


    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);
    style.setFont(ctrlStyle.font).setText(val).setFontSize(ctrlStyle.fontSize);

    Andrei
    My Envato portfolio.

  • TIneke Van Schalkwyk

    August 11, 2020 at 10:55 am

    Thanks Andrei!
    Should it work if I copy and paste exactly what you have posted?
    I get an “Error: Layer index 0 out of range”

  • TIneke Van Schalkwyk

    August 15, 2020 at 8:44 am

    So, I removed the line at the end to do with the font style as that what was causing the error to see if I could get the rest of the expression functioning correctly as a starting point.
    However, it removes the last word which is a bit of an issue ☺

    This was the line I removed incase you need to know but I don’t think that is what is causing the last word to be removed:
    style.setFont(ctrlStyle.font).setText(val).setFontSize(ctrlStyle.fontSize);

    Any ideas?

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