Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Break a line of text in half to the nearest word

  • Andrei Popa

    July 31, 2020 at 7:29 am

    Try this


    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)
    }
    breakInTwo(text.sourceText)

    Andrei
    My Envato portfolio.

  • TIneke Van Schalkwyk

    July 31, 2020 at 10:37 am

    Wow, this is absolutely brilliant!!!

    I’m also running another expression to pull in the font style but I’m not sure if/how I can have them both running:

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

  • TIneke Van Schalkwyk

    July 31, 2020 at 10:52 am

    Also, would it by any chance be possible to remove any existing line breaks first?

  • Andrei Popa

    July 31, 2020 at 11:06 am


    function breakInTwo(myString) {
    myString = myString.replace(/\r?\n|\r/g, "");
    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);
    }
    val = breakInTwo(text.sourceText);

    style.setFont(comp("00 Color Control").layer("Headline Text Font Style").text.sourceText.style.font).setText(val);

    Andrei
    My Envato portfolio.

  • TIneke Van Schalkwyk

    July 31, 2020 at 11:14 am

    I can’t thank you enough!

    Sorry to ask again… instead of removing any existing line breaks instead could it be that if there is an existing line break not to run the expression?

  • Andrei Popa

    July 31, 2020 at 11:25 am

    This were easier if you knew what you wanted from the start :))

    You can replace the last “value” with any other piece of expression you want. I say this because maybe you want to use the one with the font.


    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);
    }
    val = breakInTwo(text.sourceText);

    if (text.sourceText.indexOf("\n") == -1 || text.sourceText.indexOf("\r") == -1) {
    style.setFont(comp("00 Color Control").layer("Headline Text Font Style").text.sourceText.style.font).setText(val);
    } else {
    value;
    }

    Andrei
    My Envato portfolio.

  • TIneke Van Schalkwyk

    July 31, 2020 at 11:44 am

    So sorry, I’m completely new to this.
    That seems to still add the line break when there is already a linebreak present…

  • Andrei Popa

    July 31, 2020 at 12:31 pm

    I was joking, don’t worry, i’m here to help! ?

    I made a mistake in the lase expression. Try this one. Again, you can replace “value” with some other expression if you need to.


    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) {
    value;
    } else {
    val = breakInTwo(text.sourceText);
    style.setFont(comp("00 Color Control").layer("Headline Text Font Style").text.sourceText.style.font).setText(val);
    }

    Andrei
    My Envato portfolio.

  • TIneke Van Schalkwyk

    July 31, 2020 at 1:09 pm

    That works a charm!

    Would it be easy to put in another statement that if the text length is less than say 20 characters no line break is added?

  • Andrei Popa

    July 31, 2020 at 1:19 pm

    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<20) {
    value;
    } else {
    val = breakInTwo(text.sourceText);
    style.setFont(comp(“00 Color Control”).layer(“Headline Text Font Style”).text.sourceText.style.font).setText(val);
    }

    Andrei
    My Envato portfolio.

Page 1 of 2

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