ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Create Line Break After X Number of Characters

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Bryce Poole
Create Line Break After X Number of Characters
on Aug 8, 2014 at 4:09:15 pm

Hello,

I have a lower third that I've created in after effects that I would like to be editable in Premiere using CC 2014's new template text feature. There are two lines on the lower third and I'd like to create a text box that automatically moves to the next line when it reaches the edge of the graphic. There doesn't appear to be a way to create a line break in the text from After Effects inside Premiere. I know I could just create second text box in after effects but I'd really like it to just show up as one field for the editor inside of Premiere. I'm trying to create an expression that would automatically add line breaks to a source text layer after x number of characters, but can't quite figure out how to make it to work.

If anyone has any thoughts on how to make this possible I'd greatly appreciate the help!

Thanks,
– Bryce P.

// rough though for the code

txt = value
if (thisComp.layer("Overlay TXT Here").text.sourceText.length >= 61)
txt + "\r"

// the bit I'm missing here is how to tell after effects to inset the "\r"
at a specific position in the string


Return to posts index

Bryce Poole
Re: Create Line Break After X Number of Characters
on Aug 8, 2014 at 7:47:04 pm

Okay, this appears to have solved the issue, incase anyone else is trying to do something similar.

txt = value
if (thisComp.layer("Overlay TXT Here").text.sourceText.length >= 61) {
txt.substring(0,61) + "\r" + txt.substring(62,999);
} else {
txt
}


Return to posts index

Dan Ebberts
Re: Create Line Break After X Number of Characters
on Aug 8, 2014 at 10:31:17 pm

That works as long as you don't have to worry about splitting the text in the middle of a word, or having a line with one character, etc. If you need to address those types of issues, the solution becomes more complex.

Dan



Return to posts index


Bryce Poole
Re: Create Line Break After X Number of Characters
on Aug 9, 2014 at 12:57:38 am

Hey Dan,

Preventing it from splitting in the middle of a word is actually something I'd really like it if the code could handle, I was doing some test this afternoon and was just offsetting any breaks mid word with extra spaces before to it down to the next line, but thats really clunky...

Do you have any thoughts on a more elegant way to write this?

Thank you so much!
— Bryce P.


Return to posts index

Dan Ebberts
Re: Create Line Break After X Number of Characters
on Aug 9, 2014 at 1:12:37 am

I just crammed it into what you have, but this would be one way (I'm sure there are others):


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



Return to posts index

Bryce Poole
Re: Create Line Break After X Number of Characters
on Aug 11, 2014 at 11:44:10 pm

Dan, this is excellent, this code makes it so much cleaner than what I had, I really appreciate the help. Thank you very much!!


Return to posts index


Olga Povarchuk
Re: Create Line Break After X Number of Characters
on Dec 18, 2015 at 11:11:10 pm

Hi Dan or anyone else that can help,

I tried this expression in my own project and it works pretty well.. I modified it to create a line break after the 10th character. Only problem is it disables right when it hits character 10. At 9 it's fine, at 11 it's fine, but at 10 in doesn't seem to know what to do.

Doing my best to figure it out but unfortunately I'm relatively new to this and my debugging abilities are limited. Hopefully it's a simple fix. Any help would be greatly appreciated!

Olga

txt = comp("EDIT_THIS_COMP").layer("Text").text.sourceText;
if (comp("EDIT_THIS_COMP").layer("Text").text.sourceText.length >= 10){
for (i = 10; i > 0; i--) if (txt[i] == " ") break;
if (i > 0)
txt.substr(0,i) + "\r" + txt.substr(i+1)
else
txt.substring(0,10) + "\r" + txt.substring(11,999);
}else{
txt
}


Return to posts index

Rombout Versluijs
Re: Create Line Break After X Number of Characters
on Mar 24, 2016 at 4:49:30 am

sorry guys to bother, im just starting with this scripting. Ive used a code to generate dynamic text in a textfield on the stage, this gets filled by comment text of a marker. Im using this to add onscreen comments for the audio people without me having to keyframe everything.

txt = value;
n = 0;
if (thisComp.marker.numKeys > 0){n = thisComp.marker.nearestKey(time).index;
if (thisComp.marker.key(n).time > time) n--;
if (n > 0) txt = thisComp.marker.key(n).comment;
}
txt;


This is the code which grabbs the text from the comment section in a marker. When i combine this with the above for line breakes it stops working. Ive tried making a second variable and putting it below the loop, but didnt work


Return to posts index

Mårten Gunge
Re: Create Line Break After X Number of Characters
on May 23, 2016 at 2:18:21 pm

That is a nice piece of code. How would this look if you want three or four lines? Lots of else ifs or is there maybe a neater solution?


Return to posts index


Stephen Pickering
Re: Create Line Break After X Number of Characters
on Oct 25, 2018 at 3:55:45 pm

[Mårten Gunge] ". How would this look if you want three or four lines? Lots of else ifs or is there maybe a neater solution?"

I know this is an old post, but did you ever figure this out? I'm wondering the same thing.

Thanks!
Stephen



Return to posts index

Kalleheikki Kannisto
Re: Create Line Break After X Number of Characters
on Oct 30, 2018 at 6:05:52 pm

This should work for any number of lines. Ripped it from an old project of mine and simplified some. Set the char_per_line variable to the number of characters you want. The text will break only on the space character.

The esc variable is a precautionary measure to avoid endless loops, for example in those cases where there is no space character. I think -- I don't remember the details of the logic. But you may need to set it higher if your text needs to break onto more than 100 lines.

txt = value;
char_count = txt.length;
char_per_line = 60;
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
Senior Graphic Designer


Return to posts index

Isaac Carlson
Re: Create Line Break After X Number of Characters
on Nov 20, 2018 at 9:00:04 pm

Hi!

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


Return to posts index


Kalleheikki Kannisto
Re: Create Line Break After X Number of Characters
on Nov 21, 2018 at 9:45:54 pm

For 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


Return to posts index

Isaac Carlson
Re: Create Line Break After X Number of Characters
on Nov 27, 2018 at 9:47:42 pm

Thank you so much! I love when the easy fix works. I really appreciate your feedback. This works perfectly.

All the best,
Isaac Carlson


Return to posts index

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