Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Finding the number of lines in a block of paragraph text

  • Finding the number of lines in a block of paragraph text

    Posted by Nick Hill on March 4, 2019 at 5:42 pm

    I’m trying to write an expression that resizes a shape layer depending on the size of a paragraph text layer.

    I could just use sourceRectAtTime() *but* if there’s only one line, I want to size the box to the width of the text. If it runs over two or more lines, the box should be a fixed (maximum) width. With non-paragraph text you can just count the number of carriage returns –


    L = thisComp.layer("text_layer");
    tx = L.text.sourceText.valueAtTime(time);
    h = tx.split('\r');
    n = h.length; // how many lines we've got

    But I can’t think of a way to interrogate the paragraph text layer to find out how many lines it’s running to. Is there one, or will this be a case of manually keyframing?

    Oleg Pirogov replied 7 years, 5 months ago 2 Members · 1 Reply
  • 1 Reply
  • Oleg Pirogov

    March 5, 2019 at 1:06 am

    The usual workaround to get the number of lines is to divide sourceRect height by line height: https://forums.creativecow.net/thread/227/40793

    But since you are interested in sourceRect itself, you may duplicate your text layer and apply Animator for Character Value = 1 with Expression Selector textIndex<=4 ? 0 : 100 based on lines.

    What it does is it replaces all characters in lines after [for ex.] 4th with #1 Unicode character which is not displayed in AE (in your system too, hopefully). Effectively it means that only first 4 lines are displayed with sourceRect adjusted accordingly.

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