ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Break a line of text in half to the nearest word

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
TIneke van Schalkwyk
Break a line of text in half to the nearest word
on Jul 31, 2020 at 3:51:23 am

Wanting to simply add a line break 1/2 way in my text (I'm guessing character count). Not mid-word though, to the nearest space I guess.


Return to posts index

Andrei Popa
Re: Break a line of text in half to the nearest word
on Jul 31, 2020 at 7:29:55 am

Try this


function breakInTwo(myString) {
var mid = Math.floor(myString.length / 2);
var before = myString.lastIndexOf(" ", mid);
var after = myString.indexOf(" ", mid + 1);

if (before == -1 || (after != -1 && mid - before >= after - mid)) {
mid = after;
} else {
mid = before;
}
return myString.substring(0,mid)+"\n"+myString.substring(mid+1)
}
breakInTwo(text.sourceText)


Andrei
My Envato portfolio.


Return to posts index

TIneke van Schalkwyk
Re: Break a line of text in half to the nearest word
on Jul 31, 2020 at 10:37:01 am

Wow, this is absolutely brilliant!!!

I'm also running another expression to pull in the font style but I'm not sure if/how I can have them both running:

style.setFont(comp("00 Color Control").layer("Headline Text Font Style").text.sourceText);


Return to posts index


TIneke van Schalkwyk
Re: Break a line of text in half to the nearest word
on Jul 31, 2020 at 10:52:52 am

Also, would it by any chance be possible to remove any existing line breaks first?


Return to posts index

Andrei Popa
Re: Break a line of text in half to the nearest word
on Jul 31, 2020 at 11:06:16 am


function breakInTwo(myString) {
myString = myString.replace(/\r?\n|\r/g, "");
var mid = Math.floor(myString.length / 2);
var before = myString.lastIndexOf(" ", mid);
var after = myString.indexOf(" ", mid + 1);

if (before == -1 || (after != -1 && mid - before >= after - mid)) {
mid = after;
} else {
mid = before;
}
return myString.substring(0, mid) + "\n" + myString.substring(mid + 1);
}
val = breakInTwo(text.sourceText);

style.setFont(comp("00 Color Control").layer("Headline Text Font Style").text.sourceText.style.font).setText(val);


Andrei
My Envato portfolio.


Return to posts index

TIneke van Schalkwyk
Re: Break a line of text in half to the nearest word
on Jul 31, 2020 at 11:14:28 am

I can't thank you enough!

Sorry to ask again... instead of removing any existing line breaks instead could it be that if there is an existing line break not to run the expression?


Return to posts index


Andrei Popa
Re: Break a line of text in half to the nearest word
on Jul 31, 2020 at 11:25:39 am

This were easier if you knew what you wanted from the start :))

You can replace the last "value" with any other piece of expression you want. I say this because maybe you want to use the one with the font.


function breakInTwo(myString) {
var mid = Math.floor(myString.length / 2);
var before = myString.lastIndexOf(" ", mid);
var after = myString.indexOf(" ", mid + 1);

if (before == -1 || (after != -1 && mid - before >= after - mid)) {
mid = after;
} else {
mid = before;
}
return myString.substring(0, mid) + "\n" + myString.substring(mid + 1);
}
val = breakInTwo(text.sourceText);

if (text.sourceText.indexOf("\n") == -1 || text.sourceText.indexOf("\r") == -1) {
style.setFont(comp("00 Color Control").layer("Headline Text Font Style").text.sourceText.style.font).setText(val);
} else {
value;
}


Andrei
My Envato portfolio.


Return to posts index

TIneke van Schalkwyk
Re: Break a line of text in half to the nearest word
on Jul 31, 2020 at 11:44:39 am

So sorry, I'm completely new to this.
That seems to still add the line break when there is already a linebreak present...


Return to posts index

Andrei Popa
Re: Break a line of text in half to the nearest word
on Jul 31, 2020 at 12:31:36 pm

I was joking, don't worry, i'm here to help! 😃

I made a mistake in the lase expression. Try this one. Again, you can replace "value" with some other expression if you need to.


function breakInTwo(myString) {
var mid = Math.floor(myString.length / 2);
var before = myString.lastIndexOf(" ", mid);
var after = myString.indexOf(" ", mid + 1);

if (before == -1 || (after != -1 && mid - before >= after - mid)) {
mid = after;
} else {
mid = before;
}
return myString.substring(0, mid) + "\n" + myString.substring(mid + 1);
}

if (text.sourceText.indexOf("\n") != -1 || text.sourceText.indexOf("\r") != -1) {
value;
} else {
val = breakInTwo(text.sourceText);
style.setFont(comp("00 Color Control").layer("Headline Text Font Style").text.sourceText.style.font).setText(val);
}


Andrei
My Envato portfolio.


Return to posts index


TIneke van Schalkwyk
Re: Break a line of text in half to the nearest word
on Jul 31, 2020 at 1:09:29 pm

That works a charm!

Would it be easy to put in another statement that if the text length is less than say 20 characters no line break is added?


Return to posts index

Andrei Popa
Re: Break a line of text in half to the nearest word
on Jul 31, 2020 at 1:19:55 pm

function breakInTwo(myString) {
var mid = Math.floor(myString.length / 2);
var before = myString.lastIndexOf(" ", mid);
var after = myString.indexOf(" ", mid + 1);

if (before == -1 || (after != -1 && mid - before >= after - mid)) {
mid = after;
} else {
mid = before;
}
return myString.substring(0, mid) + "\n" + myString.substring(mid + 1);
}

if (text.sourceText.indexOf("\n") != -1 || text.sourceText.indexOf("\r") != -1 || text.sourceText.length<20) {
value;
} else {
val = breakInTwo(text.sourceText);
style.setFont(comp("00 Color Control").layer("Headline Text Font Style").text.sourceText.style.font).setText(val);
}

Andrei
My Envato portfolio.


Return to posts index

TIneke van Schalkwyk
Re: Break a line of text in half to the nearest word
on Aug 1, 2020 at 4:49:39 am

Works great but unfortunately now the font style is not working...


Return to posts index

Andrei Popa
Re: Break a line of text in half to the nearest word
on Aug 1, 2020 at 7:39:19 am

You mean when there are line breaks in the text?
This is what it was built like. I told you you could replace the "value" with another piece of expression.

So if this is the case, this should work.


function breakInTwo(myString) {
var mid = Math.floor(myString.length / 2);
var before = myString.lastIndexOf(" ", mid);
var after = myString.indexOf(" ", mid + 1);

if (before == -1 || (after != -1 && mid - before >= after - mid)) {
mid = after;
} else {
mid = before;
}
return myString.substring(0, mid) + "\n" + myString.substring(mid + 1);
}

if (text.sourceText.indexOf("\n") != -1 || text.sourceText.indexOf("\r") != -1 || text.sourceText.length<20) {
style.setFont(comp("00 Color Control").layer("Headline Text Font Style").text.sourceText.style.font);
} else {
val = breakInTwo(text.sourceText);
style.setFont(comp("00 Color Control").layer("Headline Text Font Style").text.sourceText.style.font).setText(val);
}


Andrei
My Envato portfolio.


Return to posts index

TIneke van Schalkwyk
Re: Break a line of text in half to the nearest word
on Aug 1, 2020 at 10:17:44 am

Sorry, no I meant this part is not working:

style.setFont(comp("00 Color Control").layer("Headline Text Font Style").text.sourceText.style.font).setText(val);


Return to posts index

Andrei Popa
Re: Break a line of text in half to the nearest word
on Aug 1, 2020 at 11:28:44 am

I tested it like this. I made 2 text layers for text. I added this slightly modified version for the second one. It works as intended. But it does not change fontsize, color or any other properties. Yet.


function breakInTwo(myString) {
var mid = Math.floor(myString.length / 2);
var before = myString.lastIndexOf(" ", mid);
var after = myString.indexOf(" ", mid + 1);

if (before == -1 || (after != -1 && mid - before >= after - mid)) {
mid = after;
} else {
mid = before;
}
return myString.substring(0, mid) + "n" + myString.substring(mid + 1);
}

if (text.sourceText.indexOf("n") != -1 || text.sourceText.indexOf("r") != -1 || text.sourceText.length<20) {
style.setFont(thisComp.layer(1).text.sourceText.style.font);
} else {
val = breakInTwo(text.sourceText);
style.setFont(thisComp.layer(1).text.sourceText.style.font).setText(val);
}


But if you also want to modify font size or other properties, you have to add some stuff to this expression.
This will be the base expression on which you can add properties.

function breakInTwo(myString) {
var mid = Math.floor(myString.length / 2);
var before = myString.lastIndexOf(" ", mid);
var after = myString.indexOf(" ", mid + 1);

if (before == -1 || (after != -1 && mid - before <= after - mid)) {
mid = after;
} else {
mid = before;
}
return myString.substring(0, mid) + "\n" + myString.substring(mid + 1);
}
ctrlStyle = comp("00 Color Control").layer("Headline Text Font Style").text.sourceText.style;
if (text.sourceText.indexOf("\n") != -1 || text.sourceText.indexOf("\r") != -1 || text.sourceText.length<20) {
style.setFont(ctrlStyle.font);
} else {
val = breakInTwo(text.sourceText);
style.setFont(ctrlStyle.font).setText(val);
}


You can add properties by adding the following options at the end of the "style.setFont(ctrlStyle.font)" or the "style.setFont(ctrlStyle.font).setText(val)" parts.


// Set the font used to Arial
style.setFont(ctrlStyle.font)

// Set the font size to 80
style.setFontSize(ctrlStyle.fontSize);

// Enable Faux Bold with a Boolean
style.setFauxBold(ctrlStyle.fauxBold)

// Enable Faux Italics with a Boolean
style.setFauxItalics(ctrlStyle.fauxItalics)

// Enable All Caps with a Boolean
style.setAllCaps(ctrlStyle.allCaps)

// Enable Small Caps with a Boolean
style.setSmallCaps(ctrlStyle.smallCaps)

// Set the Tracking as a number
style.setTracking(ctrlStyle.tracking);

// Set the Leading as a number
style.setLeading(ctrlStyle.leading);

// Enable Auto Leading with a Boolean
style.setAutoLeading(ctrlStyle.autoLeading);

// Set the Baseline Shift as a number
style.setBaselineShift(ctrlStyle.baselineShift);

// Set the Stroke Width as a number
style.setStrokeWidth(ctrlStyle.strokeColor);


The list is taken from this adress https://helpx.adobe.com/after-effects/using/expressions-text-properties.htm...


So, as an example, if you also want to take the size and fill color from the original layer, the final expression will look this way


function breakInTwo(myString) {
var mid = Math.floor(myString.length / 2);
var before = myString.lastIndexOf(" ", mid);
var after = myString.indexOf(" ", mid + 1);

if (before == -1 || (after != -1 && mid - before >= after - mid)) {
mid = after;
} else {
mid = before;
}
return myString.substring(0, mid) + "\n" + myString.substring(mid + 1);
}
ctrlStyle = comp("00 Color Control").layer("Headline Text Font Style").text.sourceText.style;
if (text.sourceText.indexOf("\n") != -1 || text.sourceText.indexOf("\r") != -1 || text.sourceText.length<20) {
style.setFont(ctrlStyle.font).setFontSize(ctrlStyle.fontSize).setFillColor(ctrlStyle.fillColor);
} else {
val = breakInTwo(text.sourceText);
style.setFont(ctrlStyle.font).setText(val).setFontSize(ctrlStyle.fontSize).setFillColor(ctrlStyle.fillColor);
}


I hope this was your problem.

Andrei
My Envato portfolio.


Return to posts index

TIneke van Schalkwyk
Re: Break a line of text in half to the nearest word
on Aug 2, 2020 at 12:16:00 pm

Thanks!

I managed to fix it by changing the font part as I needed it pulling the font from the source text of the field not the font of the text field if that makes sense. So was an easy fix.

style.setFont(comp("00 Color Control").layer("Headline Text Font Style").text.sourceText).setText(val);


Return to posts index

TIneke van Schalkwyk
Re: Break a line of text in half to the nearest word
on Aug 2, 2020 at 12:18:03 pm

I've also got another thread for how we can make a version of this that allows for three lines if you wanted to also comment there.

https://forums.creativecow.net/thread/2/1144665


Return to posts index

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