Skip to main content
Known Participant
September 30, 2025
Question

Help vertically centering multiple layers of text

  • September 30, 2025
  • 1 reply
  • 162 views

I want to dynamically center multiple (three, in this case) text layers as if they were one block, so no matter which of the three changes size (more text or font size change) the layers are, as one unit, centered vertically together. This is to be used with essential graphics, so it needs to be procedural. Also, this is text LAYERS, not just multiple-line text boxes (although this should accommodate the layers whether or not they have multiple lines).

 

I have no problem centering (or bottom- or top-aligning) individual layers using sourceRectAtTime(). I center the middle layer's anchor point and, using the various layers' heights and tops I align the top and bottom layers relative to the center. All is good, they behave nicely together--now I just want the lot of them centered on the screen/in the comp.

 

I'm thinking the way to go would be to parent the middle layer (because the top and bottom layers are relative to it) to a null, and somehow move the null's anchor point relative to the entire text block's height (top.height + mid.height + bottom.height) and then move it's position to the screen center. I don't know why, but my brain seizes up doing this.

 

Any help is appreciated!

 

 

1 reply

Dan Ebberts
Community Expert
Community Expert
September 30, 2025

Try this position expression without any parenting of the text layers. The expression expects the participating text layers to be the top layers in the timeline layer stack:

n = 3; // number of text layers participating (1 thru n)
gap = 25; // vertical gap between layers
vTotal = 0;
vAbove = 0;
for (i = 1; i <= n; i++){
  r = thisComp.layer(i).sourceRectAtTime(time,false);
  vTotal += r.height;
  if (i < index){
    vAbove += r.height;
  }
  if (i == index){
    myT = r.top;
  }
}
vTotal += (n-1)*gap;
vAbove += (index-1)*gap;
targetTop = (thisComp.height - vTotal)/2 + vAbove;
vOffset = value[1] - (targetTop - myT);
value + [0,-vOffset]
Known Participant
October 1, 2025

Thanks! I feel less bad about my brain breaking. I understand in general what's going on here but I'll need to do a little digging--I noticed it doesn't accommodate different gaps between the first and second and second and third lines, but that should be an easy fix somewhere (tomorrow).