How to display multiple lines of text in SVG?
just calculate the heights:
var drawx=part.x||0;
var drawy=part.y||0;
var fontSize=part.fontSize||14;
var lineHeight=part.lineHeight||1.25;
var style=part.style||"";
var fontFamily=part.fontFamily||"Arial";
var text=part.text.split('\n').map(function(a,i){ return '<tspan x="'+drawx+'" y="'+(drawy+fontSize*lineHeight+i*fontSize*lineHeight)+'">'+a+'</tspan>' }).join('\n');
tqrSvg+='<text x="'+drawx+'" y="'+drawy+'" style="'+style+'" font-family="'+fontFamily+'" font-size="'+fontSize+'">'+text+'</text>'
tspan is the right way to do it. And this is how:
<tspan x="10" dy="15">tspan line 1</tspan>
reference: http://tutorials.jenkov.com/svg/tspan-element.html
It looks like this will space the lines one after another without hard-coding a font size in each tspan
. Font at 15px:
<svg style="border:1px solid black" >
<text x="0" y="0" font-size="15" dy="0">
<tspan x="0" dy=".6em">tspan line 1</tspan>
<tspan x="0" dy="1.2em">tspan line 2</tspan>
<tspan x="0" dy="1.2em">tspan line 3</tspan>
</text>
</svg>
If you change the font size the lines continue to be spaced at 120%
apart from each other or 1.2em
. Font at 20px:
<svg style="border:1px solid black" >
<text x="0" y="0" font-size="20" dy="0">
<tspan x="0" dy=".6em">tspan line 1</tspan>
<tspan x="0" dy="1.2em">tspan line 2</tspan>
<tspan x="0" dy="1.2em">tspan line 3</tspan>
</text>
</svg>
Example - http://codepen.io/anon/pen/oXMVqo