Skip to main content
ethanwhipple
Known Participant
October 23, 2022
解決済み

How to modify scale of shape based on amount of characters

  • October 23, 2022
  • 返信数 5.
  • 481 ビュー

The title says it all! I am trying to modify the scale of a shape based on the amount of characters on a text layer above it. Allowing it to stay wide enough for the characters to fit. I am looking for an expression that sets the scale of the current composition layer ONLY IF there are at least 4 characters. I would set the scale using the expression;

 

if(thisComp.layer("Control").effect("Channel Control")("Menu") == 1){ thisLayer.transform.scale = [SCALEx,SCALEy];

else {

     value;

}

 

SCALEx and y being replaced with their respective counterparts.

 

Any help is greatly appreciated!

このトピックへの返信は締め切られました。
解決に役立った回答 ShiveringCactus

While the guys are right about using sourceRectAtTime, you could use split to turn the string into an array and then count the length:

var txt = thisComp.layer("My Text").text.sourceText.split('');
var scaleX = 200;
var scaleY = 125;
if (txt.length < 4) {
	[scaleX,scaleY]
} else {
	value
}

 

返信数 5

Community Expert
October 25, 2022

If you set a minimum width, then use sourceRectAtTime() to get the top and left height and width, you can compensate for the baseline shift in the text layer and paragraph justification. If you use the 2D Text box animation preset, you are 90% of the way to what you want. If the text is left justified, the box moves to the right. If the text is right justified, the box moves to the left. 

Mylenium
Legend
October 24, 2022

You can change a shape layer group anchor point as you see fit. A simple parametric approach would be:

 

mSize=property("Rectangle").size;

 

X=value[0]-mSize[0]*0.5;

Y=value[1];

 

[X,Y]

 

Adjust the references as is adequate for your project.

 

Mylenium

 

ShiveringCactus
Community Expert
Community Expert
October 24, 2022

While the guys are right about using sourceRectAtTime, you could use split to turn the string into an array and then count the length:

var txt = thisComp.layer("My Text").text.sourceText.split('');
var scaleX = 200;
var scaleY = 125;
if (txt.length < 4) {
	[scaleX,scaleY]
} else {
	value
}

 

ethanwhipple
ethanwhipple作成者
Known Participant
October 24, 2022

Is there a way to anchor the shape to the left? Meaning it will only expand to the right when more text is added?

 

Thanks!

Mylenium
Legend
October 24, 2022

As Rick already said, sourceRectAtTime() is the way to go. Basing such stuff on actual font metrics would be a nightmare, anyway, and AE doesn't offer any expression methods for doing it, so grabbing the bounding box dimensions of the rasterized content is as good as it gets. Minimum dimensions can be ensured by a simple if()else{} statement...

 

Mylenium

Community Expert
October 24, 2022

An easier approach is to use sourceRectAtTime() to control the size and even the position of a shape layer. There are lots of tutorials on the subject.

 

You could drive layer scale, but Rectangle Size is much easier to control.