ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Adjusting text length through expression

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Arjun Shrestha
Adjusting text length through expression
on Jul 1, 2018 at 2:08:41 pm

Is there anyway to auto adjust text length so that it does not exceed the composition border and gives a line break instead.


Return to posts index

Kalleheikki Kannisto
Re: Adjusting text length through expression
on Jul 1, 2018 at 4:38:52 pm

Using paragraph text would seem like the obvious solution, so I guess it doesn't apply here for some reason?

You can write an expression that checks (for instance) how wide the text output is and if beyond a certain threshold, you can then calculate the point where it should break. This is somewhat more complicated, because you then need to check backwards from that point in the text where the previous space character is located and break the text there by adding a newline character to that position. CharacterAt and SubString are the key words here.

How complex this gets depends on what is going on in your text layer, whether it is scaled, animated, 2D or 3D, etc.

Kalleheikki Kannisto
Senior Graphic Designer


Return to posts index

Scott McGee
Re: Adjusting text length through expression
on Jul 2, 2018 at 7:30:45 am

https://forums.creativecow.net/thread/227/33411

If you take a look at the above thread, although this also scales text it paragraphs it. Have a look at the expression coding for this. This would be a good place to start if you can't find anything else.


Return to posts index


Arjun Shrestha
Re: Adjusting text length through expression
on Jul 2, 2018 at 5:44:17 pm

How is this code working? Can you break it down to me. Most Importantly this code gives line break at space and not in middle of letters. I couldn't figure out how this code is working.

txt = value;
if (thisComp.layer("Text Layer").text.sourceText.length >= 61){
for (i = 61; i > 0; i--) if (txt[i] == " ") break;
if (i > 0)
txt.substr(0,i) + "\r" + txt.substr(i+1)
else
txt.substring(0,61) + "\r" + txt.substring(62,999);
}else{
txt
}


Return to posts index

Scott McGee
Re: Adjusting text length through expression
on Jul 3, 2018 at 9:36:45 am

So how the expression works

if (thisComp.layer("Text Layer").text.sourceText.length >= 61)
This is looking at length and seeing if it is larger or equal to the number of characters. If It does, then it goes to the next part.

for (i = 61; i > 0; i--) if (txt[i] == " ") break;
This then triggers and goes through all the characters up to 61 and stops at the last text break/space (If it doesn't then you'll find the line breaks words in two)

if (i > 0)
txt.substr(0,i) + "\r" + txt.substr(i+1)
else
txt.substring(0,61) + "\r" + txt.substring(62,999)
This defines which line it's breaking it to.

But this only works for two lines, if you want more lines then you'll have to adapt it. If you go to the link I sent, this does similar expression for multilines, and bases it's w


Return to posts index

Kalleheikki Kannisto
Re: Adjusting text length through expression
on Jul 3, 2018 at 4:23:40 pm

txt = value; current text saved in a variable "txt"

if (thisComp.layer("Text Layer").text.sourceText.length >= 61){ if the length of the text is more than 61 characters then

for (i = 61; i > 0; i--) if (txt[i] == " ") break; check backwards from character 61 until you find a space character

if (i > 0)
txt.substr(0,i) + "\r" + txt.substr(i+1)
if the location of the character (variable "i") is bigger than zero, add the new line (return) character between two pieces of text, one that comes before the space character at "i" and the one that comes after it.

else
txt.substring(0,61) + "\r" + txt.substring(62,999);
if no space character was found (i.e. variable "i" is 0 after running all the way through the text) add the new line character after the 61st character

}else{
txt
}
... and if it isn't longer than 61 characters, just return the original text

Kalleheikki Kannisto
Senior Graphic Designer


Return to posts index


Arjun Shrestha
Re: Adjusting text length through expression
on Jul 5, 2018 at 3:02:44 pm

Thank you for breaking down the code. It really helped me to understand what's going on


Return to posts index

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