Skip to main content
Inspiring
February 1, 2024
Question

Splitting up text into different layers based on line breaks

  • February 1, 2024
  • 2 replies
  • 4271 views

Hey all! 
I'm building a fancy template thingy again. This time I have rounded text boxes (created using Simple Choker -> Minimax -> Tint -> Roughen Edges) and that works fine. However, the text will be up to three lines long, and this box method only looks good on a single line, since it only adapts to the outline of the whole text block, not per line.

So my idea is: 
I'll have a separate text box for the input (which is invisible in the final output). I'm taking the text content from there, split it up on each manual entered line break and send each bit of text to one of three text layers.

Text input:
This is some
Text with three
Layers.

Box 1: This is some
Box 2: Text with three
Box 3: Layers

If there are only one or two lines, the other boxes won't get any text and stay invisible.
I'm a bit rusty in terms of expressions, can anyone help me out?

I think one piece of code on the source text of each box should be sufficient, reading the text, splitting by line breaks and putting that into a string array. Box one pulls from {0}, Box 1 from {1} and Box from {2}. 

Thank you!

2 replies

Adobe Employee
February 12, 2024

You have asked this about Expressions, but we have recently released in beta an ability in Scripting to set the box to auto fit based on the height of the text - so it does not go overset.

 

I am wondering if this might be something useful in your template building toolbox.

 

https://community.adobe.com/t5/after-effects-beta-discussions/new-text-box-options-available-in-scripting/td-p/14409110

 

Rather than trying to split up the lines in this way, keeping them in the same box and adapting a shape to the size of the box?

 

At the moment the box only changes in height, and from the bottom, which might be an issue for you.

 

Though the API is not supported in Expressions, and it is not exposed in the UI, you can use Scripting to set the property on the box and it will still function even though an Expression is changing the content.

 

We think it will a powerful ability, but it is early days and we are eager to get some feedback from template builders such as yourself who might try to use. it.

 

Douglas Waterfall

After Effects Engineering

Dan Ebberts
Community Expert
February 1, 2024

If your text input is in a layer named "Text Input", then you could get each line with a source text expression like this:

txt = thisComp.layer("Text Input").text.sourceText;
lines = txt.split("\r");
lines[0]

where the last line determines which line of text you get.

 

Inspiring
February 1, 2024

Sweet, thanks! That does exactly what I need. Now I only need to solve the box height for ascenders and descenders 😂

Inspiring
February 1, 2024

This modification keeps the expression from breaking when the input text doesn't have enough lines for all the expressions (it just display a blank line):

txt = thisComp.layer("Text Input").text.sourceText;
lines = txt.split("\r");
idx = 0; // which line to get
idx > lines.length - 1 ? "" : lines[idx]

Can you explain where this would be neccessary? I've tried the first expression and it works without any errors, even with just one or two lines.