8

I am using Inkscape to label a diagram. In the attached image I have shown how I would like to label:

enter image description here

In Inkscape is there a way to write a text inside a rectangle such that it is always at the center of the rectangle?

1
8
  1. Click on the page with the text tool, and type some text

  2. Draw a rectangle around it

  3. Select both, and do Text > Flow into Frame

  4. Highlight all the text with the Text tool, and click the Align Centre button along the top.

  5. Using the Select and Transform tool (F1)*, reposition the text so that it's centred.

That's it really. You can type new text inside and it will be centred. You can group it, copy and paste more copies, and edit the text. It should stay centred in the middle. You can also resize the rectangle, if you need to fit more text in. Even mutli line text will work. If you don't want to scale the stroke of the rectangle when you are resizing it, go into Edit > Preferences > Behaviour > Transforms, and deselect the "Scale stroke width" option

enter image description here

*Edit: In Inkscape 1.0.x the shortcut key is now s

16
  • Dear Mr. Billy Kerr, please correct me if I am wrong. I guess step 4 will help me to center align the text horizontally (automatically). And using step 5, I have to manually center align the text vertically. But my concern is that while executing step 5, as I would be manually aligning the text vertically, it's prone to human errors. Also the results achieved in step 4 might be lost. Is there a way out of this?
    – Aim
    Apr 10 '20 at 23:04
  • 1
    There's no way to align text vertically inside a text box unfortunately, however I suppose you could use the Align & Distribute panel (Shift+Ctrl+A), select both text and rectangle, and hit the "Centre on horizontal axis" button.
    – Billy Kerr
    Apr 10 '20 at 23:17
  • 1
    This will be easier in Inkscape 1.0, using 'Bounding Box' and 'Offset' LPEs in combination. Better to group the text and the object with the effect before moving them around, though, else it will get messed up (dislocated) pretty quickly.
    – Moini
    Apr 11 '20 at 15:15
  • @Moini - yes I already mentioned grouping them in my answer.
    – Billy Kerr
    Apr 11 '20 at 15:28
  • I wasn't referring to your answer, with the grouping hint, Billy, but to the LPEs I described.
    – Moini
    Apr 11 '20 at 20:41
1

Further to comments here, I've decided to add another answer as an update, since Inkscape 1.0 now has Live Path Effects which can be used for this. This idea was first suggested by @Moini in the comments.

Anyway, here's how to set it up.

  1. Draw a rectangle, any size.

  2. Type some text, select the text object, and copy it using Ctrl+C

  3. Select the rectangle you drew previously, and do Path > Path Effects

  4. Click the + icon in the Path Effects dialog

  5. Select the Bounding Box LPE (live path effect)

enter image description here

  1. In the Path Effects dialog hit the "Linked to Path in Clipboard" button. This will make the bounding box surround the text. Move the text, and the box will follow.

enter image description here

  1. Click the + icon again to add another Path Effect, and this time choose the Offset LPE.

enter image description here

  1. In the Path Effects dialog, increase the offset until you are happy.

You can now retype the text, and the surrounding box will expand to fit the text. You can also scale the text, and the box will expand to fit it.

enter image description here

5
  • HI, Billy Kerr, thanks. You second answer by using the Live Path Effects works. But I still have an issue is: by using the LPE, now the rectangle size is basically depend on the text boundary. When design my flow chart, I would like to make some rectangles which have the same size, but the internal text are different. So, I'd see if there is a method that which locates the text in the center of the rectangle, but when the rectangle changes, the text is always locates in the center. I may call this a constraint. Thanks.
    – ollydbg23
    Apr 4 '21 at 2:19
  • Another issue when I use the LPE method is: I can't copy/paste the whole block to make many clones. When I pastes the whole blocks, I see only the internal text get copied and pasted.
    – ollydbg23
    Apr 4 '21 at 2:21
  • @ollydbg23 Group it and you can make a copy. The text is still editable. Yeah this method adjusts the size of rectangle to fit the text. You may have to use another method. I can't think of any that would work automatically to centre the text inside rectangles of constant size. I mean, there are alignment tools in Inkscape, but you have to select and hit buttons to do it.
    – Billy Kerr
    Apr 4 '21 at 9:41
  • Thanks for the reply. I think there are two main methods which are the two answers you posted here for this question. I try to follow the question you posted in Apr 10 '20, but I think I can't get the exact result(you shown in the gif in that answer) when I follows the steps in Inkscape 1.0.2, so can you update the old answers for the latest Inkscape? Thanks.
    – ollydbg23
    Apr 4 '21 at 14:46
  • @ollydbg23 - the old answer still works for Inkscape 1.0.x. I just tested it. The only real difference is that the Select and Transform Tool shortcut is now changed to s, rather than F1. Other than that, there is no difference in the steps.
    – Billy Kerr
    Apr 4 '21 at 15:13
0

The answer using Text > Flow into Frame doesn't allow me to easily (?) vertically centre the text.

The answer using Live Path Effects will remove any rounded corners from the rectangle.

There is another way to do this that might not suit everyone, but works well enough for me:

  1. Create the Rectangle and Text.
  2. Make sure that the text is small enough to fit in the Rectangle (roughly position it inside the Rectangle).
  3. Select both of them.
  4. Object > Align and Distribute (Shift+Ctrl+A), Center on vertical axis, Center on horizontal axis.
  5. Group them (Ctrl+G).

This will require ungrouping and repeating steps 2-4 whenever you change the text, but it's much more intuitive for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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