Skip to main content
Participating Frequently
August 23, 2023
Question

Auto size shape layer to text

  • August 23, 2023
  • 3 replies
  • 717 views

Can't find this anywhere on the web so please help me out!
I want to create a shape layer (box behind a text layer) that adjusts to the text layer depending on what text I am writing.
But I only want the shape layer to adjust on the width not the height.

I already have the expression/code for a shape layer to adjust to a text layers with its width and height but not one for width only.
Somebody out there who knows?
Would be grateful!

This topic has been closed for replies.

3 replies

Mylenium
Legend
August 23, 2023

Size:

 

mLay=thisComp.layer("Text");

mRect=mLay.sourceRectAtTime();

mPadX=20; //padding width

mPadY=20; //padding height

 

X=mRect.width+mPadX*2;

Y=mRect.height+mPadY*2; //alternatively fixed value: Y=70;

 

[X,Y]

 

Position:

 

mLay=thisComp.layer("Text");

mPos=mLay.transform.position;

mRect=mLay.sourceRectAtTime();

mPadX=20; //padding width

mPadY=20; //padding height

cHalfX=thisComp.width*0.5;

cHalfY=thisComp.height*0.5;

 

X=mPos[0]-cHalfX+(mRect.width+mPadX*2)*0.5;

Y=mPos[1]-cHalfY+(mRect.height+mPadY*2)*0.5;

 

[X,Y]

 

Mylenium

Participating Frequently
August 24, 2023

Ok thanks Mylenium, I'll try that!

Participating Frequently
August 24, 2023

No sorry, the height of the shape layer still changes when I, as an an example, change a "s" to a "S" in my text layer. Have also tried to change the variables in the size code but its still the same...

Mylenium
Legend
August 23, 2023

You can literally find hundrerds of alternate examples, but here you go:

 

mLay(this Comp.layer("Text");

mRect=mLay.sourceRectAtTime();

mPadX=20; //padding width

mPadY=20; //padding height

 

X=mRect.width+mPadX*2;

Y=mRect.height+mPadY*2; //alternatively fixed value: Y=70;

 

[X,Y]

 

Mylenium

Participating Frequently
August 23, 2023

Thanks I will try that. So this does not adjust the height only the width?

Participating Frequently
August 23, 2023

I am not good at code/expressions... 
Should this be on position or size?

Mylenium
Legend
August 23, 2023

If you have the code already, you can simply dial in a fixed value for the second paramater instead of the .height(). not sure what else you need to know.

 

Mylenium

Participating Frequently
August 23, 2023

Thanks for your reply Mylenium.
The problem is I don't have the right code. This one adjusts both width and height but I just want it to adjust the width.
I cant just erase some letters inside this code I think.
See picture below: