Let’s say you design an element like an underline relative to another element like the length of text in the last line of a multi-line paragraph.
The problem is that when the length of the last line of text changes, the width of the underline shape layer will no longer look accurate.
This can be solved with an expression that gets the desired width of one element (the independent element) in order to set the width of another element (the dependent element).
Now in this case, getting the width of the last line of text is kind of tricky because After Effects doesn’t give you direct access to that as an attribute. You are able to get the overall width of the text layer, which in this case is tied to the maximum number of characters in a line. You can estimate the width of the last line as a proportion of the number of characters in the last line to the number of characters in the longest line multiplied by the overall width of the text layer.
Assume you have the following structure where there is a multi-line text field and a shape layer element that you want to resize relative to the last line of text.
Layer 1 - Multi-line text layer
Shape 1 - Shape layer you want resized relative to Layer 1
The expression is applied to the size transform attribute on the Shape 1 layer’s Contents > Shape > Shape Path > Size.
The expression sets the size of Shape 1’s width to the width of the text layer, adjusted to the same proportion as the number of characters in the last line to the number of characters in the longest line.
textLayer = thisComp.layer("Layer_1");
textScale = textLayer.scale;
textWidth = textLayer.sourceRectAtTime(textLayer).width;
scaledTextWidth = textWidth * (textScale[0]/100);
// split the multi-line text into lines and count them
s = textLayer.text.sourceText;
strings = s.split(/\n|\r/);
numlines = strings.length;
max=0;
for (i=0; i<numlines; i++){
if (max < strings[i].length) {
max = strings[i].length;
}
}
last=strings[numlines-1].length;
if (max-last > 1){
max = max-1;
}
r = last / max;
r_scaled = scaledTextWidth * r;
[r_scaled,value[1]]
To change the size of a dependent design element:
- Customize the highlighted portions of the expression above to use composition and layer names from your design
- Open up the size transform attribute for the shape layer’s Contents > Shape > Shape Path > Size; copy paste the updated expression
Comments
0 comments
Please sign in to leave a comment.