Forums › Adobe After Effects Expressions › Insert Line Break every X number of characters
-
Insert Line Break every X number of characters
-
Jonathan Reyes
December 6, 2016 at 7:19 pmI found this great expression by Dan Ebberts and Bryce Poole in the following post which creates a line break after a certain amount of characters:
https://forums.creativecow.net/thread/227/27641I was trying to modify it so it could continue to insert line breaks after X amount of characters instead of just the first time. For example, after every 20th character, insert a line break, essentially creating a bounding box via expressions with multiple lines of text each being 20 characters long. I know a little bit of expressions, but haven’t really messed with loops so I was wondering if anyone could help. Thanks!
txt = value;
if (thisComp.layer("Overlay TXT Here").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
} -
Dan Ebberts
December 6, 2016 at 7:49 pmSomething like this should work:
idx = 0;
txt = value;
outStr = "";
n = 20; // line break every n characters
while (idx < txt.length){
if (idx > 0) outStr += "\r";
outStr += txt.substr(idx,n);
idx += n;
}
outStr
Dan
-
Jonathan Reyes
December 6, 2016 at 7:55 pmThis is great, having this written out really helps me break down what’s going on here. Thanks Dan!
-
Jonathan Reyes
December 6, 2016 at 8:18 pmHere’s something tricky…in the previous linked post, you found a way to skip the line break if it was in the middle of the word. I’ve been staring at the expressions for a while trying to combine to 2 to see if I can make it break before 20 characters if the 20th character is in the middle of a word. I think I may now be trying to punch above my weight, but is something like that possible?
-
Dan Ebberts
December 6, 2016 at 8:30 pmSomething like this maybe:
txt = value;
n = 20;
outStr = "";
newLine = ""
splt = txt.split(" ")
for (i = 0; i < splt.length; i++){
if ((newLine + " " + splt[i]).length > n){
if (outStr != "") outStr += "\r";
outStr += newLine;
newLine = splt[i];
}else{
newLine += " " + splt[i];
}
}
if (newLine != ""){
if (outStr != "") outStr += "\r";
outStr += newLine;
}
outStr;
Dan
-
Jonathan Reyes
December 6, 2016 at 8:47 pmGenius. I’ll be honest, it’s gonna take a me a bit to go through and follow what’s happening, but overall it seems to work! It does however seem to be adding a space as the very first character of the text layer. I THINK it’s caused by the line that puts spaces between the split up words (newLine += ” ” + splt[i];) , but I will try and work around it. Thank you Dan for your incredibly fast and extremely useful response.
-
Dan Ebberts
December 6, 2016 at 9:05 pmYou’re right. Sorry about that. This should be better:
txt = value;
n = 20;
outStr = "";
newLine = ""
splt = txt.split(" ")
for (i = 0; i < splt.length; i++){
if ((newLine + " " + splt[i]).length > n){
if (outStr != "") outStr += "\r";
outStr += newLine;
newLine = splt[i];
}else{
if (newLine != "") newLine += " ";
newLine += splt[i];
}
}
if (newLine != ""){
if (outStr != "") outStr += "\r";
outStr += newLine;
}
outStr;
Dan
-
Avinash Ramanath
December 21, 2016 at 11:18 amHi Dan,
While this is working great, is there a way to keep the text centralized to the comp.
-
Andrei Popa
May 13, 2019 at 1:33 pmApply this to the Anchor point
var left = sourceRectAtTime(time,false).left;
var top = sourceRectAtTime(time,false).top;
var myWidth = sourceRectAtTime(time,false).width;
var myHeight = sourceRectAtTime(time,false).height;
var x = left + myWidth/2;
var y = top + myHeight/2;
[x,y]Andrei
My Envato portfolio.
Log in to reply.