19

I frequently create labels that follow the same format: white text on a 75% opaque black background, with 10px of margin around the text. Currently, I have to manually build these labels, which takes a lot of work.

In one method, I write the text, figure out its height and width, then create a separate rectangle and place it behind it. This takes a lot of clicking around for every label.

In the other method, I draw out a text box, select it directly, give it the correct background and margins, and then insert the text. However, I must then resize the box manually, meaning more clicking around and greater imprecision. There doesn't seem to be a way to directly select a text box once it has text inside it.

I tried using the script contained in a related answer to resize the text box to the content, but it only resizes the box vertically, and removes my color and margins.

Does anyone have suggestions on how to resolve these problems, or for a better method? I'm using CS3.

2 Answers 2

21

You really don't need any scripting for this.

  • Select text object with Selection Tool (Black arrow)
  • Add a new fill via Appearance Panel. You must add a new fill. This will not work on the default fill for type characters.
  • Move the new fill below the <Characters> item in the Appearance Panel
  • Highlight the new fill and choose Effect > Convert to Shape > Rectangle
  • Enter relative amount of points/pixels/inches etc. you want the rectangle to be offset from the text
  • Click OK
  • Drag the text object to Graphic Styles Panel

label

The background rectangle is relative to the size of the text object. Should the text object change in size the rectangle adjusts to match.

When you need to reapply the appearance, select the text object and click the Graphic Style.

If you find you are having difficulty with text colors being incorrect after applying the Graphic Style, please see @BANG's solution HERE.


If you want a stroke rather than a fill, it's the same basic steps. See here: Illustrator: Auto-sized Text box with a rectangle border (no fill)?

15
  • Nice hack! Has that been there since CS3 (which the OP is using)? Jul 25, 2013 at 23:02
  • Yes. It's been around since about Illustrator 7.
    – Scott
    Jul 26, 2013 at 0:59
  • 1
    Geesh. Look what I've been missing. Jul 26, 2013 at 15:37
  • 1
    when i do this the first instance works. The text is black and the text box is filled blue. i then go to apply the graphic style to other text boxes, and the text and text box become blue, meaning i cant see the text. what am i doing wrong? thanks in advance
    – user52505
    Oct 14, 2015 at 13:21
  • 1
    @CU3ED You can also apply Effect > Distort & Transform > Transform to that same fill in the Appearance Panel to move it/offset it. I will often move the fill 1 or 2pts up to compensate for built in leading in the font. By using the effect, it will get saved to the Graphic Style as well. So no need to adjust all the type or create any character styles for a baseline shift.
    – Scott
    Oct 19, 2020 at 21:44
2

I feel like I have to say this a lot with Adobe products but ...

The most efficient answer is scripting.

You need to get the bounds of the text, draw a box based on those numbers, and position it behind the text. With some basic math and an understanding of how to get to Illustrator's various scripting objects, it's not that tough.

For starters, you need to get the points defining the object's location:

activeDocument.selection[0].geometricBounds

That will return the array [x1, y1, x2, y2]. From there you can start building and styling your box and move it behind the text object.

You could start with the script that you linked as a reference of how it's done. If you don't know javascript and Illy's scripting interface it will take some time. The upside is that the operation will take seconds each subsequent time and you'll have a whole new world of efficiency opened up to you.

The reason that script didn't do what you wanted is that you can't apply a background color to area text. It converts it to area text to do the centering, leaving you without your box styles.

2
  • 1
    I'll leave this answer here for kicks, but I'm happy to say that Scott has rendered it unnecessary. Jul 25, 2013 at 23:04
  • Not unnecessary. I have the same question and I can't use area text so I'm going to have to script it which I've done in several Adobe items but never Illy.
    – AutoBaker
    Sep 21, 2021 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.