I am using Inkscape to label a diagram. In the attached image I have shown how I would like to label:
In Inkscape is there a way to write a text inside a rectangle such that it is always at the center of the rectangle?
Graphic Design Stack Exchange is a question and answer site for Graphic Design professionals, students, and enthusiasts. It only takes a minute to sign up.
Sign up to join this communityI am using Inkscape to label a diagram. In the attached image I have shown how I would like to label:
In Inkscape is there a way to write a text inside a rectangle such that it is always at the center of the rectangle?
Click on the page with the text tool, and type some text
Draw a rectangle around it
Select both, and do Text > Flow into Frame
Highlight all the text with the Text tool, and click the Align Centre button along the top.
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
*Edit: In Inkscape 1.0.x the shortcut key is now s
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.
Draw a rectangle, any size.
Type some text, select the text object, and copy it using Ctrl+C
Select the rectangle you drew previously, and do Path > Path Effects
Click the + icon in the Path Effects dialog
Select the Bounding Box LPE (live path effect)
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.
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:
This will require ungrouping and repeating steps 2-4 whenever you change the text, but it's much more intuitive for me.