Activity › Forums › Adobe After Effects Expressions › Adjusting text length through expression
-
Adjusting text length through expression
Posted by Ethan Dolan on July 1, 2018 at 2:08 pmIs there anyway to auto adjust text length so that it does not exceed the composition border and gives a line break instead.
Ethan Dolan replied 7 years, 10 months ago 3 Members · 6 Replies -
6 Replies
-
Kalleheikki Kannisto
July 1, 2018 at 4:38 pmUsing 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 -
Scott Mcgee
July 2, 2018 at 7:30 amhttps://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.
-
Ethan Dolan
July 2, 2018 at 5:44 pmHow 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
} -
Scott Mcgee
July 3, 2018 at 9:36 amSo 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
-
Kalleheikki Kannisto
July 3, 2018 at 4:23 pmtxt = 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 thenfor (i = 61; i > 0; i--) if (txt[i] == " ") break;check backwards from character 61 until you find a space characterif (i > 0)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.
txt.substr(0,i) + "\r" + txt.substr(i+1)elseif 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
txt.substring(0,61) + "\r" + txt.substring(62,999);}else{… and if it isn’t longer than 61 characters, just return the original text
txt
}Kalleheikki Kannisto
Senior Graphic Designer -
Ethan Dolan
July 5, 2018 at 3:02 pmThank you for breaking down the code. It really helped me to understand what’s going on
Reply to this Discussion! Login or Sign Up