Responsive Auto Resize text layers in After Effects

Together, we’ll walk through a step-by-step guide on easily auto-resizing text layers to a custom size using expressions. No more manually adjusting your text when it stretches too wide—just a few lines of code will do the trick! We’ll simplify your workflow and create awesome auto-resizing text layers that adapt automatically when they reach a set limit. Let’s dive in and make your designs even more efficient and dynamic!

Thanks to this and these amazing videos I’ve combined 2 techniques into 1 useful expression.

The main goal of this expression is to automatically scale a text layer so that it fits within a certain portion of the composition’s width and height. This is particularly useful when you want your text to stay within specific boundaries without manually adjusting its size.

  1. Define Maximum Width and Height
    JavaScript
    maxW = thisComp.width * 0.7;
    maxH = thisComp.height * 0.7;

    maxW and maxH: These variables set the maximum width and height that the text layer can occupy. They are calculated as 70% of the composition’s width and height, respectively. Adjusting the 0.7 value allows you to control how much of the comp’s dimensions the text should fill.

    2. Get the Text Layer’s Current Dimensions

    JavaScript
    r = sourceRectAtTime(time);
    w = r.width;
    h = r.height;

    r: This variable captures the rectangle (bounding box) that encloses the text layer at the current time.w and h: These represent the current width and height of the text layer based on its content.

    3. Calculate the Scaling Factor

    JavaScript
    s = w/h > maxW/maxH ? maxW/w : maxH/h;

    s: This is the crucial part of the expression. It calculates a scaling factor based on the aspect ratio of the text layer and the defined maximum dimensions (maxW and maxH).

    • If the text is wider relative to its height than the defined maximum aspect ratio, it scales down to fit within the maximum width (maxW).
    • Otherwise, it scales down to fit within the maximum height (maxH).

    4. Limit the Scaling Factor

    JavaScript
    n = s > 1 ? 1 : s;

    n: This ensures that the text layer doesn’t scale up if it’s already within the desired limits. If the calculated scaling factor s is greater than 1 (meaning the text is smaller than the allowed maximum size), it sets n to 1, keeping the text at its current size. Otherwise, it uses the scaling factor s.

    5. Apply the Scaling

    JavaScript
    [100, 100] * n;

    This final line scales the text layer uniformly by the factor n. The [100, 100] refers to the original 100% scale of the layer, and multiplying it by n adjusts the scale accordingly.

    Full code that needs to be added to Scale of Text ayer:

    JavaScript
    maxW = thisComp.width*0.7;
    maxH = thisComp.height*0.7;
    r = sourceRectAtTime(time);
    w = r.width;
    h = r.height;
    s = w/h > maxW/maxH ? maxW/w: maxH/h;
    n = s > 1 ? 1 : s;
    [100, 100]*n;

    This expression intelligently resizes a text layer to ensure it stays within a specified portion of the composition’s dimensions while preserving its aspect ratio. It prevents the text from overflowing the defined boundaries, automatically adjusting the scale based on the text’s content and the composition’s size. This approach is great for maintaining consistent and responsive designs in After Effects.

    Leave a Reply

    Your email address will not be published. Required fields are marked *