Activity › Forums › Adobe After Effects Expressions › Create Line Break After X Number of Characters
-
Create Line Break After X Number of Characters
Auric johnson replied 5 years, 10 months ago 13 Members · 20 Replies
-
Isaac Carlson
November 20, 2018 at 9:00 pmHi!
Thank you so much for sharing your wealth of expressions knowledge with the community. I am working on a project where I need to use this line breaking technique you have provided. However, the text content is being pulled from a .csv file, as opposed to text entered directly in AE through the type tool. I’ve tried using the two expressions (one linking the source text to the csv cell, and this one you generously created) but the two will not work together.
Do you know if there is a way to modify this expression so it works even if the source text is being pulled externally, or is that asking too much of the software? Thank you!
All the best,
Isaac Carlson -
Kalleheikki Kannisto
November 21, 2018 at 9:45 pmFor an easy fix you should be able to import the text into a different (hidden) text layer and pick whip the “value” for the first line from that text layer to a new text layer where this expression creates the final visible text.
Kalleheikki Kannisto
Senior Graphic Designer -
Isaac Carlson
November 27, 2018 at 9:47 pmThank you so much! I love when the easy fix works. I really appreciate your feedback. This works perfectly.
All the best,
Isaac Carlson -
Gary Frewin
January 7, 2019 at 12:35 pmThanks for this! I am having one issues though.
I am using a reference to text from a CSV file, the comp.name chooses the row from the CSV. It works perfectly on everything, except when the char_count is less than the char_per_line i.e when there should only be a single line of text.
When this is the case, the expression returns an error saying ‘property of methods named ‘i’ in class ‘global’ is missing or does not exist’.
Had a fiddle, but can’t seem to figure this out.
var compIndex = thisComp.name.split(" - ")[0];
txt = footage("TOVDataTest.csv").dataValue([1,compIndex]);
char_count = txt.length;
char_per_line = Math.ceil(thisComp.layer("Controllers").effect("Characters per line")("Slider"));
lines = Math.ceil(char_count/(char_per_line));
done = 0;
esc = 0;
len =0;
row = char_per_line;
while (done == 0){
esc ++;
len+=row;
if (txt.length >= len+1){
for (i = len; i > len-row; i--) if (txt[i] == " ") break;
if (i > len-row){
txt=txt.substr(0,i) + "\r" + txt.substr(i+1,9999);
}
}
len = i;
if (esc>100){done=1}
}
txt -
Kalleheikki Kannisto
January 7, 2019 at 2:31 pmMight be that it is looking for the variable “i” which is undefined when the length of the text is less than one row. Try adding
i=0;right before the while loop and see if that fixes it.
Or are you in CC2019? The javascript engine was updated in the latest version, so this may not work there.Kalleheikki Kannisto
Senior Graphic Designer -
Gary Frewin
January 7, 2019 at 2:51 pmThanks Kalleheikki! That worked perfectly (also kicking myself for not spotting it).
☺
Gary -
Paul Connors
February 7, 2019 at 3:31 pmHi Kalleheikki. Thank you very much for this bit of code. It’s fantastic. I was wondering if you might be able to help me take it a step further. I’d like to distribute the separate lines of text that your code makes onto separate text layers. So the words that fall within the 0-60 characters limit would appear on “New Text Line 1”, the characters that fall within the 61-120 limit would appear on “New Text Line 2”, 121-180 would appear on “New Text Line 3”. Etc. Does that make sense? I’ve tried everything I can think of but just can’t seem to crack it. Thank you!
-
Tomas Bumbulevičius
February 10, 2019 at 7:38 pmPaul, I would say the best way to achieve this is to split and push ‘range of text’ into array of lines. Then, linking each of array member to new text layer’s source.
By changing the ‘chars per line’ you could control how many layers would used to showcase the phrase.
Some things to consider:
1. This would require having N layers already created in the comp, and linked to text layer, which has array items.
2. If text is a continous phrase, then most likely Y alignment of text layers should be implemented.//Layer1 text source
lines = thisComp.layer("split-text-array").text.sourceText;
lines[0];//Layer2 text source
lines = thisComp.layer("split-text-array").text.sourceText;
lines[1];//LayerX text source
lines = thisComp.layer("split-text-array").text.sourceText;
lines[X];Find out more:
After Effects Tutorials: motion design, expressions, scripting. -
Wes Kennison
July 8, 2019 at 12:12 pmExpressions noob here. Thanks a ton for sharing this code Kalleheikki! Very helpful.
I wonder if you could elaborate on what might be different in the new JS engine? The line is breaking at the desired character count, but it seems to be splitting at one word intervals after that. Link below with example of what I mean.
-
Auric johnson
October 1, 2020 at 3:13 amHey, This was really useful thanks
txt = thisComp.name;
if (thisComp.name.length > 55){
for (i = 55; i > 0; i–) if (txt[i] == ” “) break;
if (i > 0)
txt.substr(0,i) + “\r” + txt.substr(i+1)
else
txt.substring(0,55) + “\r” + txt.substring(62,999);
}else{
txt
}
I tested it and had that same issue with the string length being exact so I removed the = from the >= seems to work okay now.
Reply to this Discussion! Login or Sign Up