Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Center align text for mogrt

  • Center align text for mogrt

    Posted by Daniel Johnson on April 15, 2024 at 8:15 pm

    I’m creating a mogrt that has to adhere to strict brand guidelines for alignment, tracking, and leading. We want the text to auto-align horizontally and vertically regardless of the number of lines. Originally the leading was 1.2x the font size. I cobbled together the following anchor point expression from forum posts (like this https://creativecow.net/forums/thread/capture-number-of-lines-in-paragraph-text-for-expr/) and it seemed to work:// Get number of lines of text// “The autoleading value is 1.2. And sometimes when leading was different value but then set to autoleading, the expression took the value before the autoleading. So I have put that condition. If autoleading is true, than leading is fontsize*1.2″const layer = thisLayer;var newStyle = layer.text.sourceText.style;var myLeading = newStyle.autoLeading ? newStyle.fontSize * 1.2 : newStyle.leading;H = layer.sourceRectAtTime(time, false).height;numberOfLines = Math.ceil(H / myLeading);numberOfLines; // Get the descender-less size of the text layerconst { fontSize, leading } = layer.text.sourceText.style;const { width } = layer.sourceRectAtTime();const xHeight = fontSize / 2;const height = xHeight + leading * (numberOfLines – 1);const trueTextSize = [width, height]; // Center the anchor point to the text layer, ignoring descendersconst { left } = layer.sourceRectAtTime();const topLeft = [left, -xHeight];topLeft + trueTextSize / 2; Now the client wants to change the leading to 0.9x font size. When I change the leading to that in the character panel, the vertical alignment gets messed up. The anchor point stays centered in the comp but the text layer moves beneath it. I can’t figure out how to keep the anchor point centered in both the comp AND the layer. Any ideas?

    David Conklin replied 2 years ago 2 Members · 1 Reply
  • 1 Reply
  • David Conklin

    May 2, 2024 at 9:34 pm

    I saw there were no replies here so I wanted to jump in.

    I may be misreading, but it’s possible you’re overcomplicating things. If you just want to keep an anchor point centered in your text layer, regardless of its settings in the character panel or position in the comp, you can use this expression on the anchor point property:

    const { top, left, width, height } = sourceRectAtTime(time-inPoint, false);
    [left + width/2, top + height/2];

    I am assuming because of the complexity of your problem that you need the anchor point to stay exactly at the baseline of type (I’m assuming that’s what the “ignore descenders” bits are about).

    If you have an even # of lines this numerically centers the layer vertically. If you have an odd # of lines this sticks the anchor point at the baseline of the middle line.

    A text layer’s anchor point will always be y=0 at the baseline of line 1. Knowing that, we can use the leading to “count down” the lines (l * numLines)

    const { top, left, width, height } = sourceRectAtTime(time-inPoint, false);
    const s = text.sourceText.getStyleAt(0,0);
    const l = s.autoLeading ? s.fontSize * 1.2 : s.leading;
    const numLines = 1 + Math.floor(height/l);
    // const bottom = top + (l * numLines);
    const xPos = left + width/2;
    const yPos = (numLines%2==0) ? top + height/2 : l * Math.floor(numLines/2);
    [xPos, yPos];

    Like I said it’s possible I’m misreading but hopefully there are some snippets in there that help you get where you need to be.

    Good luck!


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