Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Keeping my text vertically centered

  • Keeping my text vertically centered

    Posted by Nathan Watson on April 9, 2019 at 1:18 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
    }

    outStr
    }

    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!

    Thanks,
    Nathan.

    Nathan Watson replied 7 years, 5 months ago 2 Members · 2 Replies
  • 2 Replies
  • Kalleheikki Kannisto

    April 10, 2019 at 7:08 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

  • Nathan Watson

    April 10, 2019 at 4:39 pm

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

    value-[0,this.sourceRectAtTime(time).height/4]

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