Activity › Forums › Adobe After Effects Expressions › Break a line of text in half to the nearest word
-
Break a line of text in half to the nearest word
Posted by TIneke Van Schalkwyk on July 31, 2020 at 3:51 amWanting 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.
TIneke Van Schalkwyk replied 5 years, 9 months ago 2 Members · 16 Replies -
16 Replies
-
Andrei Popa
July 31, 2020 at 7:29 amTry 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. -
TIneke Van Schalkwyk
July 31, 2020 at 10:37 amWow, 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);
-
TIneke Van Schalkwyk
July 31, 2020 at 10:52 amAlso, would it by any chance be possible to remove any existing line breaks first?
-
Andrei Popa
July 31, 2020 at 11:06 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. -
TIneke Van Schalkwyk
July 31, 2020 at 11:14 amI 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?
-
Andrei Popa
July 31, 2020 at 11:25 amThis 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. -
TIneke Van Schalkwyk
July 31, 2020 at 11:44 amSo sorry, I’m completely new to this.
That seems to still add the line break when there is already a linebreak present… -
Andrei Popa
July 31, 2020 at 12:31 pmI 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. -
TIneke Van Schalkwyk
July 31, 2020 at 1:09 pmThat 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?
-
Andrei Popa
July 31, 2020 at 1:19 pmfunction 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.
Reply to this Discussion! Login or Sign Up