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