ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Dynamic text scale/fontsize up or down base on text length

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Bas v Breugel
Dynamic text scale/fontsize up or down base on text length
on Jul 13, 2018 at 12:44:35 pm

Hi,

I've searched the forum, and found this similar post.
Scaling text to text box expression
But the text layer in my project is based on a JSON file, and that doesn't seem to work with this expression setup.

So the idea is to have the text size down if it doesn't fit in the width of the white square. I guess have the expression on the scale parameter works best, you then can parent other text layers so the scale with it?




This was Kalle's take on it in above mentioned post.
txt = value;
margin = effect("margin")("Slider");
char_count = txt.length;
lead_ratio = effect("leading ratio")("Slider");
rect_height = thisComp.layer("Rect").height*thisComp.layer("Rect").scale[1]/100-2*margin;
rect_width = thisComp.layer("Rect").width*thisComp.layer("Rect").scale[0]/100-2*margin;
rect_ratio = rect_width/rect_height;
char_per_line = Math.round(Math.sqrt(char_count*rect_ratio*lead_ratio));
lines = Math.ceil(char_count/(char_per_line));
done = 0;
esc = 0;
len =0;
//row = Math.floor(thisComp.layer("Rect").sourceRectAtTime(time).width*thisComp.layer("Rect").scale[0]/1220);//divisor depends on type size
row = char_per_line;
while (done == 0){
esc ++;
len+=row;
if (txt.length >= len+1){
for (i = len; i > len-row; i--) if (txt[i] == " ") break;
if (i > len-row){
txt=txt.substr(0,i) + "\r" + txt.substr(i+1,9999);
}
}
len = i;
if (esc>100){done=1}
}
txt


Return to posts index

Kalleheikki Kannisto
Re: Dynamic text scale/fontsize up or down base on text length
on Jul 23, 2018 at 6:01:18 am
Last Edited By Kalleheikki Kannisto on Jul 23, 2018 at 6:02:43 am

This case is significantly simpler. All you need for the scale code in the c_LastName text layer is:

textwidth = thisLayer.sourceRectAtTime().width;
s = 100*550/textwidth;
[s,s]


("550" is the width of the box you want to limit to. Replace with your desired value.)

Then you can parent the first name to it. What you want to have is point text, so that it doesn't wrap onto a new line when it reaches the width of the box like it will with paragraph text.

Moving the lastname text layer anchor point to the position of the first name top left corner (while the expression is disabled) will keep the alignment at that point.

You may also want to set a maximum value for the scale (such as 100), so that it doesn't scale up for shorter names. Min expression works for this. The second line would the become:

s = Math.min(100*550/textwidth, 100);

This will help avoid massively large names when they are short, as well as the potential issue of a longer first name (with short last name) extending outside the box.

Kalleheikki Kannisto
Senior Graphic Designer


Return to posts index

Bas v Breugel
Re: Dynamic text scale/fontsize up or down base on text length
on Jul 25, 2018 at 1:46:29 pm

Hienoa! Paljon kiitoksia Kalleheikki! 😁👍


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2018 CreativeCOW.net All Rights Reserved
[TOP]