Hello Arun,
I would use a textbox to automatically limit the horizontal length of the lines, and have a hidden textlayer that tries different fontsizes untill it finds thethreshold of where it becomes a three line text.
Like this:
– make a text-box that has the horizontal size that you want to wrap at, but make it plenty tall so it fits at least 3 lines.
– give the sourceText this expression:
maxFontSize=100;
fs = maxFontSize - timeToFrames(time);
style.setFontSize(fs);
Starting from 0, it will try all integer fontsize starting from your desired maximum/default fontsize.
– give this text-layer a slider control effect, I renamed it to “optimal FontSize” and give it this expression:
posterizeTime(0);
maxFontSize=100;
function estimateLines(l,t) {
st = l.text.sourceText.getStyleAt(1,t);
ld = st.autoLeading ? st.fontSize*1.2 : st.leading;
return Math.ceil(l.sourceRectAtTime(t).height/ld);
}
for (f=0; f<maxFontSize; f++) {
t=framesToTime(f);
if (estimateLines(thisLayer, t)<=2) break
}
text.sourceText.getStyleAt(1,t).fontSize;
(alternatively this could be done witha binary search algoritmh instead of a frame by frame scan)
The resulting fontSize is the first one fits in exactly 2 lines.
You can hide the current layer, and use the calculation in a second text-layer like this:
The second Textlayer should also be a textbox with the desired wrapping width and this expression for sourceText:
posterizeTime(0);
txt = thisComp.layer("input txt").text.sourceText;
st = thisComp.layer("input txt").text.sourceText.style;
st
.setFontSize(thisComp.layer("input txt").effect("optimal FontSize")("Slider").value)
.setAutoLeading(false)
.setLeading(effect("leading")("Slider").value)
.setText(txt);
as you see, it now references the contents of the original text, the optimal fontsize, and a new effects slider where you can dial in the desired leading