2022-01-16 18:03:50 +01:00
|
|
|
---
|
|
|
|
title: Handling whitespace in text
|
|
|
|
---
|
|
|
|
|
|
|
|
When [adding text to your pattern](/howtos/code/adding-text) you might
|
|
|
|
need to add a special type of text: whitespace.
|
|
|
|
|
|
|
|
Whitespace in patterns can either be line breaks, or spaces. We'll
|
|
|
|
show you have to handle both below:
|
|
|
|
|
2022-12-25 21:32:18 -08:00
|
|
|
## Adding line breaks to text
|
2022-01-16 18:03:50 +01:00
|
|
|
|
2022-12-25 21:32:18 -08:00
|
|
|
To add line breaks to text, you merely have to include them in your text.
|
2022-12-22 17:24:59 -08:00
|
|
|
When doing so, keep in mind that single-quoted strings in JavaScript
|
2022-12-25 21:32:18 -08:00
|
|
|
will **not** pick up line breaks.
|
2022-01-16 18:03:50 +01:00
|
|
|
|
2022-10-12 14:42:45 +02:00
|
|
|
<Example caption="An example of whitespace in text">
|
|
|
|
```design/src/part.mjs
|
|
|
|
function draftPart = ({
|
|
|
|
Point,
|
|
|
|
points,
|
|
|
|
Path,
|
|
|
|
paths,
|
|
|
|
part
|
|
|
|
}) {
|
|
|
|
points.demo1 = new Point(10,20)
|
|
|
|
// highlight-start
|
|
|
|
.addText('this\nwill\nwork')
|
|
|
|
// highlight-end
|
|
|
|
points.demo2 = new Point(40,20)
|
|
|
|
// highlight-start
|
|
|
|
.addText("this\nwill\nalso\nwork")
|
|
|
|
// highlight-end
|
|
|
|
points.demo3 = new Point(70,20)
|
|
|
|
// highlight-start
|
|
|
|
.addText(`And
|
|
|
|
this
|
|
|
|
will
|
|
|
|
also
|
|
|
|
work`).attr('data-text-lineheight', 7)
|
|
|
|
// highlight-end
|
2022-01-16 18:03:50 +01:00
|
|
|
|
2022-10-12 14:42:45 +02:00
|
|
|
// Prevent clipping
|
|
|
|
paths.diag = new Path()
|
|
|
|
.move(new Point(0,10))
|
|
|
|
.move(new Point(90, 70))
|
2022-01-16 18:03:50 +01:00
|
|
|
|
2022-10-12 14:42:45 +02:00
|
|
|
return part
|
|
|
|
}
|
2022-01-16 18:03:50 +01:00
|
|
|
```
|
2022-10-12 14:42:45 +02:00
|
|
|
</Example>
|
2022-01-16 18:03:50 +01:00
|
|
|
|
2024-09-28 13:13:48 +02:00
|
|
|
:::tip
|
2022-10-12 14:42:45 +02:00
|
|
|
You can control the lineheight by setting the `data-text-lineheight` attribute.
|
2024-09-28 13:13:48 +02:00
|
|
|
:::
|
2022-01-16 18:03:50 +01:00
|
|
|
|
2022-10-12 14:42:45 +02:00
|
|
|
## Adding consecutive spaces to text
|
2022-01-16 18:03:50 +01:00
|
|
|
|
|
|
|
Adding a single space between two words is not a problem.
|
|
|
|
But what if you want to add a couple of spaces in a row?
|
|
|
|
Both in HTML and SVG they will get collapsed into a single space.
|
|
|
|
|
2022-10-12 14:42:45 +02:00
|
|
|
To get around that, use ` ` for space.
|
2022-01-16 18:03:50 +01:00
|
|
|
|
2022-10-12 14:42:45 +02:00
|
|
|
```mjs
|
|
|
|
points.demo = new Point(0, 0)
|
|
|
|
// highlight-start
|
|
|
|
.addText('far      apart')
|
|
|
|
// highlight-end
|
|
|
|
}
|
2022-01-16 18:03:50 +01:00
|
|
|
```
|
|
|
|
|