Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Make a box around Text auto-adjust size

  • Make a box around Text auto-adjust size

    Posted by Nate Vander plas on November 10, 2011 at 11:28 pm

    I have some text with a box (shape layer Rectangle) around it that I would like to automatically resize based on the size of the text. Mostly I just need it to resize the width since I’ll only have one line at a time and won’t be changing the font. I pieced some stuff together from other posts (see below), applying it to the Rectangle Path Size and setting the text to center align. Unfortunately it’s not very accurate. It works for some text, but can get very far off depending on number of spaces and such. Also, I’d like be able to have the text left aligned if possible. Maybe an expression on the scale of the shape layer? With the anchor point set to the same place as the text anchor point?

    Thanks,
    Nate

    s = thisComp.layer("Our Historical Foundation 2").text.sourceText.length * 55;
    s2 = content("Rectangle 1").content("Rectangle Path 1").size[1];
    [s, s2]

    Marcin Staszewski replied 9 years, 11 months ago 4 Members · 9 Replies
  • 9 Replies
  • Dan Ebberts

    November 11, 2011 at 12:19 am

    Unfortunately, there’s no good way for an expression to get the extents of the actual text. There is a hack using sampleImage() (you can find it in the archives of this forum) that will give you the left and right edges of the text, which you should be able to use to scale your background layer horizontally.

    You can do it very precisely with a script, but of course that doesn’t have a live update, so if you edit the text you have to run the script again.

    Dan

  • Nate Vander plas

    November 11, 2011 at 12:33 am

    I’ll try that. Thanks Dan.

  • Marcin Staszewski

    May 14, 2016 at 1:51 pm

    I know this is a very old thread, but I was trying to figure out the same thing for the last few days, and in case anyone else is wondering how to get it done, Motion Boutique has a free script called text box! Look it up, it works like a charm, but only if you have one line of text, or all your lines have the same width.

    In my case I have a different sentence on every line and I need the box to be just the same as my text (with some padding). I don’t want to divide my lines into separate text, cause there’s too much of it.

    I created a precomp with text sourced to the original one in the main comp, and then there is about 25 copies each with a different character offset setting. This fills out the lines with mulitiple letters (there are still some gaps tough). In my main comp I put these effects on my precomp (which I set to draft quality, to get straight edges of the filled lines):
    Mosaic – trying to find a perfect setting to cut out rough edges and pointy characters
    Curves – boosting alpha contrast, to get rid of semi transparent parts of edges

    It kinda works, but sometimes the letters like “J” and “y” stick out too much and mosaic effect sees them and creates a tile outside the text line, which ruins the effect…

    Anyone has a better solution?

  • Kalleheikki Kannisto

    May 14, 2016 at 5:22 pm

    This was just answered yesterday in this thread:

    https://forums.creativecow.net/thread/227/32772

  • Marcin Staszewski

    May 14, 2016 at 9:48 pm

    I’m sorry, but I can’t get your expression to work. I created some text layer, which I named “my_text”, and underneath I placed a shape layer and applied your expressions. I get “Function thisComp.layer().sourceRectAtTime is undefined” error.

  • Kalleheikki Kannisto

    May 15, 2016 at 7:11 am

    The shape layers needs to have a rectangle drawn in it. The expression is applied to the rectangle size and position, not to the shape layer itself. Place the rectangle in the right position in relation to the text.

  • Marcin Staszewski

    May 15, 2016 at 7:20 am

    I did exactly what you just said. Seems more like there an spelling error in the expression itsel? It says the function is undefined.

    When I erase “sourceRectAtTime()” it doesn’t give me any errors, however the rectangle gets width and height of the main comp regardless of my text size.

  • Kalleheikki Kannisto

    May 26, 2016 at 5:20 pm

    That functionality was introduced in AECC2014.2, won’t work with an earlier version.

  • Marcin Staszewski

    May 26, 2016 at 6:39 pm

    Thanks, I’ll give it a try, I have both CS6 and CC, and only tried it in CS6 so far.

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy