ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Keeping my text vertically centered

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Nathan Watson
Keeping my text vertically centered
on Apr 9, 2019 at 1:18:50 pm

So I currently have this expression on my text:

idx = 0
var txt = text.sourceText

if (txt.indexOf(' ') > 0 ){

txt = txt.replace(/\s/g, '')
sp = text.sourceText
outStr = ""
n = sp.indexOf(" ")
while (idx < txt.length){
if (idx > 0) outStr += "\r"
outStr += txt.substr(idx,n)
idx += n


It's taking the sentence I enter and putting every new word on a new line.

What I need to add now is something to make sure the text stays vertically centered with every new line added. I've tried doing something like this
var a = 0;
var b = 2.9285;
transform.anchorPoint = [a,b];

but that only works when it is placed in the specific anchor point expression rather than the one on the source text.

Any questions or help is much appreciated!


Return to posts index

Kalleheikki Kannisto
Re: Keeping my text vertically centered
on Apr 10, 2019 at 7:08:57 am

In your case you could offset the Y position of the text layer (in the negative direction) by multiplying the line height by (number of lines minus 1)/2. You can get the number of lines by splitting the original text to an array at each space and getting the length of the resulting array.

Kalleheikki Kannisto
Senior Graphic Designer

Return to posts index

Nathan Watson
Re: Keeping my text vertically centered
on Apr 10, 2019 at 4:39:35 pm

Perfect, got it working, cheers!
Had to divide by 4 because of my use case but this works for me


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2019 All Rights Reserved