Activity › Forums › Adobe After Effects Expressions › Dynamically Distribute Text Layers Horizontally
-
Dynamically Distribute Text Layers Horizontally
Posted by Christian Simpson on February 17, 2023 at 3:27 pmHello All,
I am a bit stumped. I am trying to evenly distribute the x position of 4 text layers horizontally all on the same y axis in a composition. I would like to for the group of 4 to also be centered within the comp. And for the cherry on top I would love to have a slider to control the gap distance between layers for tweaking as needed.
Does anyone have any insights on how to accomplish something like this? I’ve attempted several solutions with sourceRecTime and have been successful so far.
Dan Ebberts replied 2 years, 4 months ago 4 Members · 9 Replies -
9 Replies
-
Filip Vandueren
February 17, 2023 at 4:17 pmAre they all single line texts ? Are the differences in ascenders and descenders from layer to layer the problem ?
-
Christian Simpson
February 17, 2023 at 4:20 pmSingle line yes, and there are descenders and ascenders.
-
Christian Simpson
February 17, 2023 at 5:35 pmThe ascenders and decenders aren’t even the issue yet, I haven’t even made it that far.
-
Christian Simpson
February 17, 2023 at 8:42 pmI tried using this on each layer but it still doesnt space evenly.
gap = thisComp.layer("Control").effect("Spacing")("Slider")+(thisComp.layer("A").transform.xPosition)
L = thisComp.layer("A");
rect = L.sourceRectAtTime();
[gap+rect.width+rect.left]; -
Dan Ebberts
February 18, 2023 at 7:19 amI think something like this might get you started. Set nLayers to the number of text layers participating (they need to be contiguous) and set firstLayer to the index of the first participating text layer.
gap = thisComp.layer("controls").effect("Gap")("Slider");
nLayers = 4;
firstLayer = 1;
curEdge = 0;
wTotal = gap*(nLayers-1);
for(i = firstLayer; i < firstLayer+nLayers; i++){
L = thisComp.layer(i);
r = L.sourceRectAtTime(time,false);
w = r.width;
wTotal += w;
if (i == index) myOffset = r.left;
if (i < index) curEdge += w + gap;
}
x = thisComp.width/2 - wTotal/2 + curEdge - myOffset;
[x,value[1]] -
Christian Simpson
April 8, 2023 at 1:24 amNot sure if this is possible but would there be a way to make it so that, if for example, these 4 pieces of text end up being wider than a certain predetermined amount the group as a whole scales accordingly to fit back into a predetermined width?
-
Megan Au
March 5, 2024 at 2:12 amThis is great! How would you amend this so it everything is anchored left at a point (instead of centred to comp), and works for both text and shape layers? Thanks!
-
Dan Ebberts
March 5, 2024 at 8:01 pmIf I understand what you’re asking, this should work:
xPos = thisComp.layer("controls").effect("Left X")("Slider");
gap = thisComp.layer("controls").effect("Gap")("Slider");
firstLayer = 1;
curEdge = 0;
for(i = firstLayer; i <= index; i++){
L = thisComp.layer(i);
r = L.sourceRectAtTime(time,false);
w = r.width;
if (i == index) myOffset = r.left;
if (i < index) curEdge += w + gap;
}
x = xPos + curEdge - myOffset;
[x,value[1]]
Reply to this Discussion! Login or Sign Up