ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Insert Line Break every X number of characters

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Jonathan Reyes
Insert Line Break every X number of characters
on Dec 6, 2016 at 7:19:37 pm

I 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/27641

I 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
}


Return to posts index

Dan Ebberts
Re: Insert Line Break every X number of characters
on Dec 6, 2016 at 7:49:31 pm

Something 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



Return to posts index

Jonathan Reyes
Re: Insert Line Break every X number of characters
on Dec 6, 2016 at 7:55:58 pm

This is great, having this written out really helps me break down what's going on here. Thanks Dan!


Return to posts index


Jonathan Reyes
Re: Insert Line Break every X number of characters
on Dec 6, 2016 at 8:18:37 pm

Here'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?


Return to posts index

Dan Ebberts
Re: Insert Line Break every X number of characters
on Dec 6, 2016 at 8:30:58 pm

Something 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



Return to posts index

Jonathan Reyes
Re: Insert Line Break every X number of characters
on Dec 6, 2016 at 8:47:11 pm

Genius. 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.


Return to posts index


Dan Ebberts
Re: Insert Line Break every X number of characters
on Dec 6, 2016 at 9:05:28 pm

You'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



Return to posts index

Jonathan Reyes
Re: Insert Line Break every X number of characters
on Dec 6, 2016 at 9:27:11 pm

That did the trick, thanks Dan!


Return to posts index

Avinash Ramanath
Re: Insert Line Break every X number of characters
on Dec 21, 2016 at 11:18:07 am

Hi Dan,
While this is working great, is there a way to keep the text centralized to the comp.








Return to posts index

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