Hey Johan. There are quite a few scenarios, but in general:
1. Create a null and add two Sliders, where one will control X padding, and other for Y.
2. Create two Text layers and two rectangular shape layers.
3. Inside the shape layers rect size, add expression similar to this, based on your layers/controllers names:
textLayer = thisComp.layer(1);
textS = textLayer .transform.scale[0] / 100;
textW = textLayer .sourceRectAtTime(time,true).width*textS;
textH = textLayer .sourceRectAtTime(time,true).height*textS;
boxW = textW+thisComp.layer('control').effect('box-xPadding')('Slider');
boxH = textH+thisComp.layer('control').effect('box-yPadding')('Slider');
[boxW, boxH]
}
Where textLayer should be your main text layer’s index.
4. Apply this expression to second shape’s size as well, and change textLayer index to your subtitles one.
5. You will have to adjust positioning of the layers and their anchor points as well as add another slider to control the gap between them.
I assume in the forum there is already a fully working solution, so searching could help.
Find out more:
After Effects Tutorials: motion design, expressions, scripting.
Boxer – Dynamic Text Boxes Template with a Live Preview