SVG 1.2 Tiny添加了文本换行,但是您在浏览器中看到的大多数SVG实现(Opera除外)都没有实现这个特性。这通常取决于您(开发人员)手动定位文本。
SVG 1.1规范很好地概述了这一限制,以及克服它的可能解决方案:
Each ‘text’ element causes a single
string of text to be rendered. SVG
performs no automatic line breaking or
word wrapping. To achieve the effect
of multiple lines of text, use one of
the following methods:
The author or authoring package needs
to pre-compute the line breaks and use
multiple ‘text’ elements (one for each
line of text).
The author or authoring
package needs to pre-compute the line
breaks and use a single ‘text’ element
with one or more ‘tspan’ child
elements with appropriate values for
attributes ‘x’, ‘y’, ‘dx’ and ‘dy’ to
set new start positions for those
characters who start new lines.
(This approach allows user text
selection across multiple lines of
text -- see Text selection and
clipboard operations.)
Express the
text to be rendered in another XML
namespace such as XHTML [XHTML]
embedded inline within a
‘foreignObject’ element. (Note: the
exact semantics of this approach are
not completely defined at this time.)
http://www.w3.org/TR/SVG11/text.html#Introduction
As a primitive, text wrapping can be simulated by using the dy attribute and tspan elements, and as mentioned in the spec, some tools can automate this. For example, in Inkscape, select the shape you want, and the text you want, and use Text -> Flow into Frame. This will allow you to write your text, with wrapping, which will wrap based on the bounds of the shape. Also, make sure you follow these instructions to tell Inkscape to maintain compatibility with SVG 1.1:
https://wiki.inkscape.org/wiki/Frequently_asked_questions#What_about_flowed_text.3F
此外,还有一些JavaScript库可以用来动态自动化文本包装:
https://old.carto.net/papers/svg/textFlow/
值得注意的是,CSVG将形状包装到文本元素的解决方案(例如,参见他们的“按钮”示例),尽管重要的是要提到他们的实现在浏览器中不可用:
https://users.monash.edu/~clm/csvg/about.html
我提到这一点是因为我已经开发了一个csvg启发的库,它允许您做类似的事情,并且可以在web浏览器中工作,尽管我还没有发布它。