Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Auto horizontally center lines of dynamic text boxes

  • Auto horizontally center lines of dynamic text boxes

    Posted by John Burgess on April 6, 2024 at 3:07 pm

    I’m trying to find a solution to aligning multiple text box rows horizontally to the center of the comp.
    I have a bunch of these I need to create and ideally don’t want to be manually spacing them.

    I’m using this expression on the position to create equal margins between each box though if text is edited it works from left to right throwing the spacing out.

    s = thisComp.layer(“Controller”).effect(“Spacing”)(“Slider”);

    prev = thisComp.layer(index – 1);

    x = prev.transform.position[0];

    y = prev.transform.position[1];

    w = prev.sourceRectAtTime().width;

    [x + w + s, y]

    Each text box also has a position text animator applied so each can move independently without effecting the others.

    Brie Clayton
    replied 5 months ago
    3 Members · 3 Replies
  • 3 Replies
  • Julian Chojnacki

    April 7, 2024 at 3:52 pm

    Hi John, try this:

    1. Create a Null with 2 sliders called Total Width & Spacing, and apply the following expression to the Total Width slider:

    const spacing = effect("Spacing")("Slider").value;

    let totalWidth = 0;

    let rectTime = time;

    if (thisLayer.marker.numKeys) {

    rectTime = thisLayer.marker.key(1).time;

    }

    for (let i = 1; i <= thisComp.numLayers; i++) {

    const L = thisComp.layer(i);

    if (L.hasParent && L.parent.index == thisLayer.index) {

    totalWidth += L.sourceRectAtTime(rectTime).width + spacing;

    }

    }

    totalWidth - spacing;

    2. Make sure your text layers are left-aligned, parent them to the Null, and use the following code for your text layers’ position:

    const spacing = parent.effect("Spacing")("Slider").value;

    const totalWidth = parent.effect("Total Width")("Slider").value;

    const center = thisComp.width / 2;

    let x = center - (totalWidth / 2);

    let rectTime = time;

    if (parent.marker.numKeys) {

    rectTime = parent.marker.key(1).time;

    }

    for (let i = thisComp.numLayers; i > index; i--) {

    const L = thisComp.layer(i);

    if (L.hasParent && L.parent.index == parent.index) {

    x += L.sourceRectAtTime(rectTime).width + spacing;

    }

    }

    parent.fromComp([x, parent.transform.position[1]]);

    If you want to start a new line, you can create a new Null and parent all related text layers to that Null – then you can control the line spacing with your Nulls’ Y positions.

    Regarding text animators, you can use a marker on the Null layer as the sampling time for the layout.

    LMK if that works!

  • John Burgess

    April 7, 2024 at 10:12 pm

    Thanks so much Julian, this works perfect!

  • Brie Clayton

    April 8, 2024 at 1:02 pm

    Thank you for solving this, Julian!

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