ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Capture number of lines in Paragraph Text for expression

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Troy Poirier
Capture number of lines in Paragraph Text for expression
on Jan 24, 2019 at 9:07:35 pm

I'm looking for a way to find the number of lines in a paragraph text box.

The text is dynamically generated when I create a new composition or modify the composition name. It pulls the text from TSV data in a separate composition, and all the text flows/wraps in the paragraph text to create anywhere from 1 to 8 lines of text. I have a null object with a slider that I manually input the number of lines (needed for an animation unrelated to this question), but having this variable propagate automatically would save me time, as every week I need to update this slider within multiple compositions.

I looked into the following options :

1) Range Selector : obviously knows how many lines ("selector("Range Selector 1").end") with "index" and "lines" set, but once set, the "end" number does not update with the new text.

2) Expression Selector : knows how many lines and uses the variable "textTotal" to do the math, but I cannot figure out how to extract that variable for use elsewhere in the composition.

Can't seem to find an answer to this but I feel like it should be possible; and maybe I am way off and After Effects just doesn't allow for this. Any help appreciated.


Return to posts index

Alex Printz
Re: Capture number of lines in Paragraph Text for expression
on Jan 24, 2019 at 9:27:59 pm

you could do something like measuring the height of your text box and dividing by the height of the leading between lines:

var t = thePathToTheTextObject;
var h = 12; //make this a size appropriate to the line height in pixels; can't pull dynamically from character panel;
var n = text.sourceRecAtTime(time)/h;
n.toFixed(0)

I would suggest rigging h to a slider and test until it seems right, and then keep it there until it (might) break.

Alex Printz
Mograph Designer


Return to posts index

Alex Printz
Re: Capture number of lines in Paragraph Text for expression
on Jan 24, 2019 at 9:28:45 pm

whoops, that:

var t = thePathToTheTextObject;
var h = 12; //make this a size appropriate to the line height in pixels; can't pull dynamically from character panel;
var n = t.sourceRecAtTime(time)/h;
n.toFixed(0)

Alex Printz
Mograph Designer


Return to posts index


Troy Poirier
Re: Capture number of lines in Paragraph Text for expression
on Jan 28, 2019 at 3:50:14 pm

Thank you so much, Alex! I don't know why your specific code didn't work for me, but you set me off in the right direction with sourceRectAtTime(). I found a similar chunk of code from Dan Ebberts elsewhere that led me to the solution below. Only oddity in this code was that I had to add a "padding" variable in order for the position value to match the manual setting I had before. Not a big deal, and this saves me so much time!

// text layer
t = thisComp.layer("text");

// paragraph text height
r = t.sourceRectAtTime(time,true).height;

// font line height + padding
y = 75;
p = y + 5;

// paragraph height divided by line height = # of lines
n = r/y;

// removes any decimals
n = n.toFixed(0);

/* code below spits out the Y Position values
for my 2 keyframes based on knowledge of
how many lines, up to 8 lines */

lines = 340 + p;
c = 0;

do {
lines = lines - p;
c++;
}
while (n > 1 && n &lt; 9 && n != c);

lines;

if (numKeys > 1){
k1 = key(1).time;
k2 = key(2).time;
v1 = lines;
v2 = 473;
linear(time, k1, k2, v1, v2);
} else {
value
}


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2019 CreativeCOW.net All Rights Reserved
[TOP]