Forums › Adobe After Effects Expressions › Changing Shape Layers size (width) due to text width
-
Changing Shape Layers size (width) due to text width
-
Filip Vandueren
July 23, 2020 at 9:33 amLike this (Rectangle size):
t = thisComp.layer("my_text");
tRect = t.sourceRectAtTime(time,false);
tUL = t.toComp([tRect.left,tRect.top]);
tLR = t.toComp([tRect.left+tRect.width,tRect.top+tRect.height]);
myRect = sourceRectAtTime(time,false);
myUL = toComp([myRect.left,myRect.top]);
myLR = toComp([myRect.left+myRect.width,myRect.top+myRect.height]);
textSize = t.text.sourceText.style.fontSize;
margin = textSize * 0.8;
[(tLR[0]-tUL[0] + 2*margin)/(myLR[0]-myUL[0]),(tLR[1]-tUL[1]+ 2*margin)/(myLR[1]-myUL[1])]*100;
.style just looks at the fontSize of the very first Character, so if for some layout-reason that character was set at a smaller size and you want to look at a different character, you should use this:
textSize = t.text.sourceText.getStyleAt(1).fontSize;
the index is 0-based, so this example will look at the fontSize of the second character. -
Filip Vandueren
July 23, 2020 at 9:48 amBTW,
instead of making the box with rectangle and two expressions, it is better to do it with a Path-shape:
expression for Path of a shapeLayer:
t=thisComp.layer("my_text");
sr = t.sourceRectAtTime();marginH = t.text.sourceText.style.fontSize * 0.5;
marginV = marginH * 0.75;tl = fromCompToSurface(t.toComp([sr.left - marginH, sr.top - marginV]));
tr = fromCompToSurface(t.toComp([sr.left + sr.width + marginH, sr.top - marginV]));
br = fromCompToSurface(t.toComp([sr.left + sr.width + marginH, sr.top + sr.height + marginV]));
bl = fromCompToSurface(t.toComp([sr.left - marginH, sr.top + sr.height + marginV]));createPath( [tl,tr,br,bl], [], [], true);
Add Fill, Stroke, rounded corners to taste.The upside of this method is that the Path text-box can rotate with the Text. Even if the Text is rotating in 3D.
-
Ter Ber
July 23, 2020 at 1:08 pmPerfect….absolutely perfect!
Asking for help and being blessed with someone else, whom you’ve never met and from any part of the world to collaborate and showing kindness is the most satisfying thing.Thank you SO much Filip (and everyone else here) for your contributions.
🙂 -
Ter Ber
August 1, 2020 at 12:18 pm
Log in to reply.