How to center text in a box

Post questions on how to use or achieve an effect in Inkscape.
g99k
Posts: 3
Joined: Mon Dec 14, 2009 5:24 am

How to center text in a box

Postby g99k » Mon Dec 14, 2009 6:01 am

Hi everyone. I've got a text layout question:

I want a text paragraph to be horizontally and vertically centered in a box, with word wrap and automated line breaks to make it fit. Enlarging the box shouldn't enlarge the text, but simply make it wrap in a different way. If I change the text, the new text should be centered automatically according to the new size.

It's pretty much like an HTML div with text-align:center and vertical-align:middle, only that I want it to be SVG. The "Flow into Frame" effect seems to do the trick, except for the vertical centering, which is kind of important for me.

Is there a way to achieve this with Inkscape, or with SVG in general?

Thanks in advance for any insights!

User avatar
brynn
Posts: 10309
Joined: Wed Sep 26, 2007 4:34 pm
Location: western USA
Contact:

Re: How to center text in a box

Postby brynn » Mon Dec 14, 2009 8:00 am

I'm not sure if this is the best solution, but I think it would be worth a try.
When you type out your text, use the Text and Font dialog. There is a choice whether to align your text left, right, centered or both left and right. Try choosing center, maybe that will solve your problem. Or the justify left and right option might work too, I'm not sure.

deric
Posts: 1
Joined: Thu Feb 18, 2010 1:44 am

Re: How to center text in a box

Postby deric » Thu Feb 18, 2010 2:57 am

Hi g99k,

I'm looking for the same functionality as you describe. So far I've found that most of this can be achieved with Inkscape except for the vertical centering (of horizontal text), which is also important to me.

As you mentioned, "...The "Flow into Frame" effect seems to do the trick, except for the vertical centering...". If you want to do what you requested, except for the vertical centering, and you don't want to explicitly create a new object (e.g. rectangle) and then create text and then combine them with "flow into frame", you can use the "create and edit text objects" button. Instead of just clicking at one spot on your diagram and then typing text, you should click and drag which will create a rectangular area for text. Then type your text in this rectangular area. Then, as brynn suggested, use the "Text and Font" dialog to center horizontally. You can enlarge the rectangular text area without enlarging the text - the trick to this is to make sure you select and resize the rectangular text area and not the text itself - do this by double clicking on the text. Also, you can add text and it should remain centered horizontally.

Since you mentioned "HTML div with text-align:center and vertical-align:middle", I'll assume that you might be interested in some more technical information about this. I would suggest that you use the Edit -> XML Editor... option so that you can see what's going on at the XML level. From this you can see that creating a rectangular text area seems to be almost identical to using the "flow into frame" feature to combine text and a rectangle object. Except the rectangle is invisible and instead of being a link from the flowed text it is more of an embedded rectangle.

As for SVG in general, my understanding is that what your request cannot be done with the current version of SVG which seems to be version 1.1 (see http://www.w3.org/Graphics/SVG/). Flowed text (I don't know specifically about centering horizontally and vertically) does however seem to be supported in an old draft for SVG 1.2 (see http://www.w3.org/TR/2004/WD-SVG12-20041027/flow.html) which dates back to 2004 and I don't quite understand where it stands now. See also the Inkscape FAQ for some info on this subject (http://wiki.inkscape.org/wiki/index.php ... ed_text.3F).

Your original post is over two months old so you might not be looking for a solution anymore or maybe you've found one. I'm looking for a solution to the vertical centering problem - specifically with "flow into frame". I'm starting my enquiry here but I might post to the Bugs system (https://bugs.launchpad.net/inkscape/) as a new feature request. Or possibly post in the "Inkscape Ideas" section (viewforum.php?f=28) on the inkscapeforum.com.



Thanks,
Deric

Zom-B
Posts: 15
Joined: Tue May 08, 2012 12:06 am

Re: How to center text in a box

Postby Zom-B » Tue May 08, 2012 12:16 am

I know this is an old topic, but I haven't found an answer or workaround to this, on the forum or by searching the net, and then I accidentally found a workaround myself.

  • Create a shape and a text object. Do not flow them.
  • Open the "Align and distribute objects (Shift+Ctrl+A)" dialog.
  • Choose a "Relative to" mode that refers to an object (I prefer "Biggest object" as the text object ought to be smaller)
  • Select the objects
  • Press both horizontal and vertical align buttons.

As this is just a workaround, the downside is that whenever you edit the text, you have to repeat the procedure. But hey, at least it looks better than the default top alignment.

Aladin3
Posts: 12
Joined: Sun Mar 07, 2010 5:55 am
Location: Slovakia

Re: How to center text in a box

Postby Aladin3 » Tue May 08, 2012 9:38 pm

to Zom-B:

I am still curious why Inkscape does not support some scripting ability, even with some autoupdate functionality. I this case I create macro for all step for centering of text in box, then aply it to my text + box and set autoupdate flag (checkbox). So even I change text it will still stay centered. ;-)

User avatar
brynn
Posts: 10309
Joined: Wed Sep 26, 2007 4:34 pm
Location: western USA
Contact:

Re: How to center text in a box

Postby brynn » Fri May 11, 2012 5:45 am

I'm not sure what you mean, Aladin3. Inkscape certainly an be scripted. I personally don't know how, but I know it can be done. I'm not sure what a "macro" is, but it sounds a lot like a script.

User avatar
shawnhcorey
Posts: 149
Joined: Mon Jan 07, 2008 12:17 pm

Re: How to center text in a box

Postby shawnhcorey » Fri May 11, 2012 10:57 pm

Inkscape is a drawing program, not a word processor. It cannot wrap text. Inkscape is designed around SVG and SVG does not allow for auto-wrapping. Until it does, Inkscape will not provide it. I suggest you contact W3C and tell them to add text wrapping to SVG.


Return to “Help with using Inkscape”