In After Effects, I have a string, I have a TypeFace, I have the width and height of a composition. How do I get the right font size to fit the string on the comp, without overflowing?
Noobie programmer here.
You could calculate the pixel size however (again) Adobe restricts the size to 1296 Px. So you need to go by scale of your text layer.
In scale parameter of the text add following expression:
var myTextSize = sourceRectAtTime();
var ratio = myTextSize.width / myTextSize.height;
var compRatio = thisComp.width/thisComp.height;
if(ratio >= compRatio) {
var factor = thisComp.width / myTextSize.width;
[scale[0] * factor, scale[1] * factor]
}
else{
var factor = thisComp.height / myTextSize.height;
[scale[0] * factor, scale[1] * factor]
}
This expression scales the text depending on width and height of your comp.
However it is still not centered. If you want that, you need to add another expression. This time on your anchorpoint:
var boundingbox = sourceRectAtTime();
[boundingbox.left + boundingbox.width/2, boundingbox.top + boundingbox.height/2 ];
This should do the trick.