Text
Titles, labels, paragraphs: text is in every interface. JOID builds it from three pieces: a style that names a font, the text itself, and a node that draws it.
You will learn
- How to show a text and describe its style
- How to align a text in a box, or label any node
- How to handle a text longer than its box
- How to mix styles on one line and keep a text up to date
- How to measure a text
Show a text
Section titled “Show a text”A TextNode draws a Text, which pairs a string with a TextInfo, its style:
TextNode.create(50, 50).text(Text.create("Hello JOID", title)).attach(card);Created by TextNode.create(x, y), without a size, the node takes the size of its text.
The style names a font that your application loads once: Fonts shows how.
Describe a style
Section titled “Describe a style”TextInfo.create takes a font and a size in design pixels; each setter changes one more property:
final TextInfo title = TextInfo.create(ExampleFont.REGULAR, 56F).weight(FontWeight.BOLD).color(GreetingUI.TEXT);final TextInfo body = TextInfo.create(ExampleFont.REGULAR, 28F).color(GreetingUI.MUTED);final TextInfo caption = body.withFontSize(20F).withLetterSpacing(0.15F);
| Setter | Changes |
|---|---|
fontSize(float) | The size, in design pixels. |
weight(...) | The weight, a FontWeight from THIN (100) to BLACK (900): Fonts explains which face it draws. |
italic(boolean) | The italic face, or the upright face slanted when the font has none. |
color(...) | The color of the glyphs, a Color (Colors and styling). |
letterSpacing(float) | The space added between letters, as a share of the size (1F = 100 %): 0.15F adds 15 % of the size. |
lineHeight(float) | The height of a line, as a share of the size (1F = 100 %): 1.5F is 150 %; 0F keeps the height of the font. |
shadow(), shadow(Color) | Adds a shadow under the text; shadow() follows the color of the text, 30 % darker, and shadow(Color) keeps a fixed color. |
TextInfo lists the other settings, such as the offset and the tint of the shadow.
The markup and the effects belong to Text markup and effects.
Each setter changes the TextInfo itself, and every text built on it follows.
Each one has a with... twin, such as TextInfo.withFontSize, that leaves the style as it is and returns a changed copy: the caption above derives from the body without changing it.
LockableValue.lock makes a shared style safe: its setters then throw an IllegalStateException, and its with... copies still work (Keep a palette safe).
Align a text in a box
Section titled “Align a text in a box”The alignment belongs to the Text: Text.alignX and Text.alignY take an Align (START, CENTER or END), and Text.align sets both.
Give the TextNode a size, and the text aligns inside it:
RectNode.create(150, 50, 300, 80).color(AlignUI.ELEMENT).body(button -> TextNode.create(0, 0, 300, 80).text(Text.create("Play", info).align(Align.CENTER)).attach(button)).attach(card);The text node covers the button, so “Play” stays at its center whatever the font.
Any node can also carry a label: Node.label draws a Text centered on the node, above what the node draws and below its children.
RectNode.create(150, 170, 300, 80).color(AlignUI.ELEMENT).label(Text.create("Settings", info)).attach(card);
label(...) needs no extra node.
A text node in the box places the text anywhere else in it, with the alignments of the Text.
Handle a long text
Section titled “Handle a long text”By default, a TextNode draws its text on one line and never cuts it.
A TextMode, given to TextNode.mode, changes that inside the width of the node:
TextNode.create(40, 60, 440, 40).text(Text.create(sentence, info).overflow(TextOverflow.ELLIPSIS)).mode(TextMode.OVERFLOW).attach(card);TextNode.create(520, 60, 440, 40).text(Text.create(sentence, info)).mode(TextMode.SPLIT).attach(card);TextNode.create(40, 220, 440, 40).text(Text.create(sentence, info)).mode(TextMode.BOX).attach(card);TextNode.create(40, 380, 440, 40).text(Text.create(sentence, info)).attach(card);
| Mode | What it draws |
|---|---|
NORMAL | One line, never cut. A width or a height of 0 takes the size of the text. |
OVERFLOW | One line, cut at the width of the node and ended with the mark of the Text. |
SPLIT | As many lines as needed, broken at spaces, at \n or at <br>, and inside a word longer than the width; the height of the node follows the lines. |
BOX | Like SPLIT, but only the lines that fit in the height of the node. |
The mark of a cut text is a TextOverflow, given to Text.overflow: NONE (no mark), ELLIPSIS (...), DOT (.) or HYPHEN (-).
Mix styles on one line
Section titled “Mix styles on one line”A Text is a line of runs, each a TextElement with its own style:
TextNode.create(50, 50).text(Text.create(TextElement.create("Welcome back, ", info), TextElement.create("Ada", bold))).attach(card);The runs follow each other on one baseline, so a bold name sits in the line like a word of the sentence. For a style inside one string, such as a bold word in a translated sentence, use markup instead (Text markup and effects).
Keep a text up to date
Section titled “Keep a text up to date”Text.create(...) also takes a Supplier instead of a string.
JOID calls it each time it measures or draws the text, so the text follows your data:
RectNode.create(50, 140, 400, 80).color(RunsUI.ELEMENT).onClick(event -> this.clicks++).body(button -> { TextNode .create(0, 0, 400, 80) .text(Text.create(() -> "Clicks: " + this.clicks, info).align(Align.CENTER)) .attach(button);}).attach(card);A text can be any object, such as a number: JOID turns it into a string (Convert your own types).
To make the whole Text of a node follow a signal, Signals explains the reactive setters.
Measure a text
Section titled “Measure a text”A TextInfo measures a string in its style: TextInfo.getWidth gives its width, TextInfo.getHeight() the height of one line.
TextInfo.aw and TextInfo.ah(double) add a value to them, to size a box around a label:
final String label = "Settings";RectNode.create(50, 50, info.aw(label, 48D), info.ah(24D)).color(RunsUI.ELEMENT).body(box -> TextNode.create(24, 12).text(Text.create(label, info)).attach(box)).attach(card);
The box is the width of “Settings” plus 48 and the line height plus 24, so the label sits 24 design pixels from each side and 12 from the top.
A Text measures itself too, with Text.getWidth and Text.getHeight.
- A
TextNodedraws aText, a string paired with aTextInfo; without a size, it takes the size of its text. - A
TextInfoholds the font, size, weight, color, spacing and shadow; its setters change it, itswith...methods return a copy. - The alignment belongs to the
Text, inside the size of the node;label(...)centers a text on any node. TextModechooses one line (NORMAL), a cut line (OVERFLOW), wrapped lines (SPLIT) or wrapped lines in a box (BOX).TextElementruns mix styles on one line; aSupplierkeeps a text up to date.TextInfo.getWidth(...),aw(...)andah(...)measure a text to size what is around it.
Try it
Section titled “Try it”A title bar. Center the word “Settings” in a 1920 × 100 bar at the top of the canvas.
Solution
Give the bar a label, the text is centered on it:
RectNode.create(0, 0, 1920, 100).color(GreetingUI.ELEMENT).label(Text.create("Settings", info)).attach(this);A two-line summary. Show the description of an item in a 400-wide box, wrapped, and never taller than two lines.
Solution
BOX draws the lines that fit in the height of the node: give it the height of two lines.
TextNode.create(0, 0, 400, info.getHeight() * 2D).text(Text.create(description, info)).mode(TextMode.BOX).attach(card);