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
, ...). 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.
joint.util.breakText('this is quite a long text', { width: 50 })
// 'this is\nquite a\nlong\ntext'
The method accepts the following options (via opt
).
separator
- a string or a regular expression which denotes how to split the text into words. It defaults in ' '
eol
- a string representing the end of line symbol. It defaults to '\n'
.svgDocument
- SVG document that will be used to append the internally created SVG text element used for measurements (by default, an SVG document is created automatically for you).