SVG 自动换行 - 显示停止符?
为了好玩,我尝试看看在业余时间为 RIA 实现 SVG 浏览器客户端能走多远。
但遇到了似乎是一个巨大的绊脚石。 没有自动换行!!
有谁知道有什么解决办法吗(我在想某种我不知道的 JavaScript 或特殊标签)?
如果不是,我要么必须走 xhtml 路线并开始在我的 SVG 中粘贴 HTML 元素(哎呀),要么十年后 SVG 1.2 准备好时再回来。
For fun I am trying to see how far I can get at implementing an SVG browser client for a RIA I'm messing around with in my spare time.
But have hit what appears to be a HUGE stumbling block. There is no word wrap!!
Does anyone know of any work around (I'm thinking some kind of JavaScript or special tag I don't know)?
If not I'm either going to have to go the xhtml route and start sticking HTML elements in my SVG (ouch), or just come back again in ten years when SVG 1.2 is ready.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
SVGT 1.2 引入了 textArea 元素 http://www.w3.org/TR/SVGTiny12 /text.html#TextInAnArea ,但目前仅 Opera 10 实验性支持。 我不知道其他浏览器是否会计划实现它,但我希望他们会这样做。
SVGT 1.2 introduces the textArea element http://www.w3.org/TR/SVGTiny12/text.html#TextInAnArea , but it is only experimentally supported by Opera 10 at the moment. I don't know if other browsers will ever plan on implementing it, though I hope they will.
svg.js 库有一个 svg.textflow.js 插件。 它不是超快,但它确实有效。 它甚至将溢出文本存储在数据属性中,以便您可以使用它来创建连续流动的列。 这里是文本流示例页面。
The svg.js library has a svg.textflow.js plugin. It's not ultra fast but it does the trick. It even stores overflowing text in a data attribute so you can use it to create continuously flowing columns. Here the text flow example page.
另一种方法是使用 Andreas Neuman 的 文本框 对象。
An alternative method is to use Andreas Neuman's text box object.
如今,flowPara 可以自动换行,但我还没有找到正确支持它的浏览器。
These days, flowPara can do word wrapping, but I have yet to find a browser that supports it properly.
我一直在寻找有关 svg 中自动换行的解决方案几个小时(或很多天)。
如果您可以在您的应用程序中编辑代码以添加一些 tspan 或任何其他方法,请加入其中。
文本换行将在 1.2 版本中实现,但除了 Opera 之外,还没有浏览器完全实现它(4 年了,规范在 W3 上......)。
因为我必须使用一些对齐设置,所以我无法使用许多论坛可以提供的任何代码(没有异物,没有 carto 脚本或任何东西)。
如果我发布此消息,只是为了在谷歌搜索自动换行 svg 时对其他人有用,因为这篇文章位于顶部结果中,并且在许多情况下,这篇文章没有帮助。
这是一个很酷、简单且轻便的解决方案:
http://dev.w3。 org/SVG/profiles/1.1F2/test/svg/text-dom-01-f.svg
I've been looking for a solution about word wrapping in svg so many hours (or many days).
If you can in your app, edit your code to put some tspan, or any other method, go in it.
Text wrapping will be implement in the 1.2 version but except opera, no browser fully implement it yet (4 years, the specification are on the W3 ...).
Because I had to use some alignment settings, i couldn't use any of the code that many forum can provide (no foreign object, no carto script or anything).
If I post this message, it's just in order to be usefull to some other people when googling word wrapping svg because this post on the top result and in many case, this post doesn't help.
Here is a cool, easy and light solution :
http://dev.w3.org/SVG/profiles/1.1F2/test/svg/text-dom-01-f.svg
这个 SVG 东西令人困惑,不是吗?
值得庆幸的是,您可以取得一些好的结果,但这比使用 HTML 5 需要更多的工作。
这是我的 ASP.Net / SVG 应用程序的屏幕截图,其中包含一些“伪造”的自动换行。
以下函数将为您创建一个 SVG text 元素,分为 < tspan 片段,每行长度不超过 20 个字符。
它并不完美,但它简单、快速,而且用户永远不会知道其中的区别。
我的 createSVGtext() JavaScript 函数采用三个参数:x 位置、y 位置和要显示的文本。 字体、每行最大字符数和文本颜色都在我的函数中进行了硬编码,但这可以轻松更改。
要显示上面屏幕截图中显示的右侧标签,您可以使用以下方式调用该函数:
这是 JavaScript 函数:
自动换行的逻辑基于 此 HTML5 Canvas 教程
我希望您发现这很有用!
迈克
http://www.MikesKnowledgeBase.com
更新
我忘记提及一件事。
我上面显示的“工作流程图”屏幕最初只是使用 HTML 5 canvas 编写的。 它工作得很好,图标可以拖动,点击图标时会出现弹出菜单,甚至 IE8 似乎也对此很满意。
但我发现,如果图表变得“太大”(例如 4000 x 4000 像素),那么将无法在所有浏览器中初始化,不会出现任何内容 - 但是 - 就 JavaScript 代码而言担心,一切都很好。
因此,即使进行了错误检查,我的图表仍显示为空白,并且我无法检测到此问题何时发生。
因此,这就是为什么我必须重写 JavaScript 代码以使用 SVG 的原因。 它似乎可以更好地处理更大的图表。
This SVG stuff is baffling, isn't it ?
Thankfully, you can achieve some good results, but it takes more work than using the HTML 5 .
Here's a screenshot of my ASP.Net / SVG app, featuring a bit of "faked" word wrapping.
The following function will create an SVG text element for you, broken into tspan pieces, where each line is no longer than 20 characters in length.
It's not perfect, but it's simple, fast, and the users will never know the difference.
My createSVGtext() JavaScript function takes three parameters: an x-position, y-position and the text to be displayed. The font, maximum-chars-per-line and text color are all hardcoded in my function, but this can be easily changed.
To display the right-hand label shown in the screenshot above, you would call the function using:
And here's the JavaScript function:
The logic for word-wrapping is based on this HTML5 Canvas tutorial
I hope you find this useful !
Mike
http://www.MikesKnowledgeBase.com
UPDATE
One thing I forgot to mention.
That "Workflow diagram" screen that I've shown above was originally just written using an HTML 5 canvas. It worked beautifully, the icons could be dragged, popup menus could appear when you clicked on them, and even IE8 seemed happy with it.
But I found that if the diagram became "too big" (eg 4000 x 4000 pixels), then the would fail to initialise in all browsers, nothing would appear - but - as far as the JavaScript code was concerned, everything was working fine.
So, even with error-checking, my diagram was appearing blank, and I was unable to detect when this showstopper problem was occurring.
So, this is why I've had to rewrite the JavaScript code to use SVG instead. It just seems to cope better with larger diagrams.
还有foreignObject标签。 然后您可以将 HTML 嵌入到 SVG 中,这提供了最大的灵活性。 HTML 非常适合文档布局,并且已经被不断地修改以支持应用程序布局、绘图以及我们开发人员想要的一切。 但它的优势在于自动换行和文档布局。 让 HTML 做它最擅长的事情,让 SVG 做它最擅长的事情。
http://www.w3.org/TR/SVG/extend.html
这适用于大多数浏览器 FireFox、Opera、Webkit,IE 除外(自 IE11 起)。 :-( 网络故事不是吗?
There is also foreignObject tag. Then you can embed HTML in SVG which gives the greatest flexibility. HTML is great for document layout and has been hacked to no end to support application layout, drawing, and everything us developers want. But it's strength is word wrapping and document layout. Let HTML do what it does best, and let SVG do what it does best.
http://www.w3.org/TR/SVG/extend.html
This works for most browsers FireFox, Opera, Webkit, except IE (as of IE11). :-( Story of the web ain't it?
根据这个文档,似乎tspan可以给人自动换行的错觉:
Per this document, it appears that tspan can give the illusion of word wrap: