Activity › Forums › Adobe After Effects Expressions › Capture number of lines in Paragraph Text for expression
-
Capture number of lines in Paragraph Text for expression
Andrei Popa replied 1 week, 6 days ago 10 Members · 13 Replies
-
Skye Xavier
September 15, 2024 at 7:02 pmI had to do a similar thing today, but I’m counting the number of lines by using carriage returns.
Here’s the expression I used:
const wordArray = thisComp.layer("Source Text").text.sourceText.split( '\r' ); wordArray.length;
-
Arie Stavchansky
September 30, 2024 at 6:26 pmI’ve noticed that in some fonts, like Hindi, the line count is incorrect when I use the code you provided. It works perfectly in latin based fonts, but for some reason other fonts don’t calculate it correctly. I’m not sure how to fix, but just wanted to add this note for anybody working with fonts that might have non-latin characters.
-
Andrei Popa
October 1, 2024 at 8:33 amHi Arie.
I had the same problem with some arabic languages, while using the Noto fonts provided by google.
I had to change the expression because the font was too high, so I subtract half of the font size from the entire height. This still counts badly if the first row consists in only characters with very small height, like “. _ ,”, but I think that case will be very rare. You can also choose at which point in time you want to calculate the number of rows. This is especially useful when you have text animators that alter the sourceRectAtTime height, so you want to put there the timestamp where your text is finished animating.
//L=layer, T=time
function getNumRows(L, T) {
var H = L.sourceRectAtTime(L.sourceTime(T), false).height;
var myStyle = L.text.sourceText.style;
myFontSize = myStyle.fontSize;
myLeading = myStyle.autoLeading ? myFontSize * 1.2 : myStyle.leading;
return Math.ceil((H - myFontSize * 0.5) / myLeading);
}
getNumRows(thisLayer, 0)Andrei
Reply to this Discussion! Login or Sign Up