util.breakText(text, size [, attrs, opt])
Break the text
into lines so that the text fits into the bounding box defined by size.width
and, optionally, size.height
. attrs
is an object with SVG attributes that will be set on the SVG text before measuring it (such as font-weight
, font
, ...). If opt.svgDocument
is specified, that SVG document will be used to append the internally created SVG text element used for measurements (by default, an SVG document is created automatically for you). This function creates a temporary SVG <text> element with the attributes provided and continuously measures its width and height trying to fit as many words as possible on each line. When the text overflows the size.height
, it is cut. The return value is a string with automatically added newline characters (\n) so that the text fits into the bounding box specified. Useful for implementing auto-wrapping of text in JointJS shapes.
joint.util.breakText('this is quite a long text', { width: 50 })
// 'this is\nquite a\nlong\ntext'