-
Breaking up text into even lines
I would like my headlines to automatically break into multiple lines evenly instead of just wrapping the text and ending up with one word on a line.
So far I have this script but I’m losing the last word of the text when I use it.
Any ideas?function breakTextInEqualParts(myString, partNo) {
var splits = [0];
var subStrings = [];
var strLength = myString.length;
if (
partNo < 2 ||
myString == "" ||
text.sourceText.indexOf("\n") != -1 ||
text.sourceText.indexOf("\r") != -1 ||
text.sourceText.length < 20
)
return myString;for (var i = 1; i < partNo; i++) {
splits.push(Math.round(strLength / partNo) + splits[i - 1]);
}
splits.push(strLength);for (var i = 0; i < strLength - 1; i++) {
subStrings.push(breakText(myString, splits[i], splits[i + 1]));
}return subStrings.join("\n");
function breakText(myString, idx1, idx2) {
if (myString == "") return "";
idx1 = calcIndex(myString, idx1);
idx2 = calcIndex(myString, idx2);
return myString.substring(idx1, idx2);function calcIndex(myString, idx) {
if (idx == 0) return idx;
var before = myString.lastIndexOf(" ", idx);
var after = myString.indexOf(" ", idx + 1);if (before == -1 || (after != -1 && idx - before >= after - idx)) {
idx = after;
} else {
idx = before;
}
return idx;
}
}
}var len = text.sourceText.length;
if (len > 40) parts = 2;
else if (len > 20) parts = 3;
else parts = 1;var ctrlStyle = thisComp.layer(index - 1).text.sourceText.style;
val = breakTextInEqualParts(text.sourceText, parts);