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

  • TIneke Van Schalkwyk

    August 1, 2020 at 4:49 am

    Works great but unfortunately now the font style is not working…

  • Andrei Popa

    August 1, 2020 at 7:39 am

    You mean when there are line breaks in the text?
    This is what it was built like. I told you you could replace the “value” with another piece of expression.

    So if this is the case, this should work.


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

    August 1, 2020 at 10:17 am

    Sorry, no I meant this part is not working:

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

  • Andrei Popa

    August 1, 2020 at 11:28 am

    I tested it like this. I made 2 text layers for text. I added this slightly modified version for the second one. It works as intended. But it does not change fontsize, color or any other properties. Yet.


    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) {
    style.setFont(thisComp.layer(1).text.sourceText.style.font);
    } else {
    val = breakInTwo(text.sourceText);
    style.setFont(thisComp.layer(1).text.sourceText.style.font).setText(val);
    }

    But if you also want to modify font size or other properties, you have to add some stuff to this expression.
    This will be the base expression on which you can add properties.

    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);
    }
    ctrlStyle = comp("00 Color Control").layer("Headline Text Font Style").text.sourceText.style;
    if (text.sourceText.indexOf("\n") != -1 || text.sourceText.indexOf("\r") != -1 || text.sourceText.length<20) {
    style.setFont(ctrlStyle.font);
    } else {
    val = breakInTwo(text.sourceText);
    style.setFont(ctrlStyle.font).setText(val);
    }

    You can add properties by adding the following options at the end of the “style.setFont(ctrlStyle.font)” or the “style.setFont(ctrlStyle.font).setText(val)” parts.


    // Set the font used to Arial
    style.setFont(ctrlStyle.font)

    // Set the font size to 80
    style.setFontSize(ctrlStyle.fontSize);

    // Enable Faux Bold with a Boolean
    style.setFauxBold(ctrlStyle.fauxBold)

    // Enable Faux Italics with a Boolean
    style.setFauxItalics(ctrlStyle.fauxItalics)

    // Enable All Caps with a Boolean
    style.setAllCaps(ctrlStyle.allCaps)

    // Enable Small Caps with a Boolean
    style.setSmallCaps(ctrlStyle.smallCaps)

    // Set the Tracking as a number
    style.setTracking(ctrlStyle.tracking);

    // Set the Leading as a number
    style.setLeading(ctrlStyle.leading);

    // Enable Auto Leading with a Boolean
    style.setAutoLeading(ctrlStyle.autoLeading);

    // Set the Baseline Shift as a number
    style.setBaselineShift(ctrlStyle.baselineShift);

    // Set the Stroke Width as a number
    style.setStrokeWidth(ctrlStyle.strokeColor);

    The list is taken from this adress https://helpx.adobe.com/after-effects/using/expressions-text-properties.html

    So, as an example, if you also want to take the size and fill color from the original layer, the final expression will look this way


    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);
    }
    ctrlStyle = comp("00 Color Control").layer("Headline Text Font Style").text.sourceText.style;
    if (text.sourceText.indexOf("\n") != -1 || text.sourceText.indexOf("\r") != -1 || text.sourceText.length<20) {
    style.setFont(ctrlStyle.font).setFontSize(ctrlStyle.fontSize).setFillColor(ctrlStyle.fillColor);
    } else {
    val = breakInTwo(text.sourceText);
    style.setFont(ctrlStyle.font).setText(val).setFontSize(ctrlStyle.fontSize).setFillColor(ctrlStyle.fillColor);
    }

    I hope this was your problem.

    Andrei
    My Envato portfolio.

  • TIneke Van Schalkwyk

    August 2, 2020 at 12:16 pm

    Thanks!

    I managed to fix it by changing the font part as I needed it pulling the font from the source text of the field not the font of the text field if that makes sense. So was an easy fix.

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

  • TIneke Van Schalkwyk

    August 2, 2020 at 12:18 pm

    I’ve also got another thread for how we can make a version of this that allows for three lines if you wanted to also comment there.

    https://forums.creativecow.net/thread/2/1144665

Page 2 of 2

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