如何在snap svg中自动文本换行文本?

Bay*_*yan 1 javascript svg word-wrap raphael snap.svg

使用以下插件和代码,您可以将文本包装在以下位置\n:

Snap.plugin(function (Snap, Element, Paper, glob) {
    Paper.prototype.multitext = function (x, y, txt) {
        txt = txt.split("\n");
        var t = this.text(x, y, txt);
        t.selectAll("tspan:nth-child(n+2)").attr({
            dy: "1.2em",
            x: x
        });
        return t;
    };
});
Run Code Online (Sandbox Code Playgroud)

//你可以像这样使用它:

var ttt = paper.multitext(100, 100, "Sample\nmultiline\ntext").attr({
    font: "18px Arial",
    textAnchor: "middle"
}); 
Run Code Online (Sandbox Code Playgroud)

它产生:http: //jsbin.com/panaxujuta/1/edit?html,output

              Smaple
             multiline
               text
Run Code Online (Sandbox Code Playgroud)

如何使用rect Width作为长短语或段落行的限制来自动执行此过程?我不想在svg中使用foreignobject以便稍后我需要文本.请原生svg解决方案.任何想法将不胜感激.

编辑:而不是拆分\n我们如何分割文本行的某个宽度?

Edit2:我有这个代码使用上面的插件,为什么这不起作用?:

var phrase = "Etiam porttitor risus in ex blandit sodales. Ut cursus mi augue, sit amet interdum diam interdum sit amet. Nunc nec lectus ex.";
var textBoxWidth = 400;
var words = phrase.split(" ");
var newPhrase = words[0];

 var t1 = paper.multitext(10,50,newPhrase)
  .attr({
    fill: "none",
    stroke:"orange",
    "text-anchor":"start",
    "font-size":"18px",
    "font-family":"Arial"});
for(i=1;i<words.length;i++){
    t1.attr({"text":newPhrase + " " + words[i]});
    if(t1.getBBox().width<=textBoxWidth){
        newPhrase += " " + words[i];
    } else {
        newPhrase += " \n" + words[i]  ;
    } 

}
Run Code Online (Sandbox Code Playgroud)

Tho*_*mas 8

感谢发布此内容.我有一些包装到长度的代码,但是因为我没有使用数组版本而遇到麻烦text().

问题是您必须先知道字体大小才能限制字符串.我能够根据您的代码和基于/sf/answers/692955861/的包装逻辑进行此操作.它不完美,如果你缓存每个字体/大小的字母宽度可能会更快.

  Snap.plugin(function (Snap, Element, Paper, glob) {
     Paper.prototype.multitext = function (x, y, txt, max_width, attributes) {

        var svg = Snap();
        var abc = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        var temp = svg.text(0, 0, abc);
        temp.attr(attributes);
        var letter_width = temp.getBBox().width / abc.length;
        svg.remove();

        var words = txt.split(" ");
        var width_so_far = 0, current_line=0, lines=[''];
        for (var i = 0; i < words.length; i++) {

           var l = words[i].length;
           if (width_so_far + (l * letter_width) > max_width) {
              lines.push('');
              current_line++;
              width_so_far = 0;
           }
           width_so_far += l * letter_width;
           lines[current_line] += words[i] + " ";
        }

        var t = this.text(x,y,lines).attr(attributes);
        t.selectAll("tspan:nth-child(n+2)").attr({
           dy: "1.2em",
           x: x
        });
        return t;
     };
  });
Run Code Online (Sandbox Code Playgroud)

像这样使用

var bobo = paper.multitext(50, 50, "bobo bobo bobo bobo bobo bobo bobo bobo bobo", 150,
    { "font-size": "30px" });
Run Code Online (Sandbox Code Playgroud)