Skip to content

Text ​

The text display object and its effects and input events.

ts
import { Text, TextEffects, TextFieldType, TextInputEvent, TextCompositionEvent } from "willow";

Text ​

ts
class Text extends Node

Extends Node.

Text-rendering primitive. text is live-settable.

Text: constructors ​

ts
new Text(text: string)
new Text(text: string, font: Font)

Text: properties ​

MemberDescription
align: stringHorizontal text alignment. One of "left" | "center" | "right". An unknown string raises a typed VM error. Defaults to "left".
color: ColorEquivalent to label.color = color. Glyph fill colour, a Color.
fontSize: floatGlyph point size in pixels. Defaults to 16. Assigning re-flows on the next frame.
wrapWidth: floatWrap width in pixels; text wraps to fit. 0 (default) disables wrapping.
lineHeight: floatLine-spacing multiplier (1 = single-spaced, default). Must be > 0.
sharpness: floatSDF edge sharpness, 0 (default) to 1 (sharpest); values outside are clamped. Higher values narrow the anti-aliased edge of the glyphs.
pixelSnap: booleanWhole-pixel text, true (default): the first baseline and the x origin land on whole pixels when the text is not rotated or skewed, which keeps small text crisp. Set false on a label that moves slowly, so it glides instead of stepping a pixel at a time.
style: stringFont style: "regular" (default), "bold", "italic", or "boldItalic".
readonly effects: TextEffectsOutline / glow / shadow parameters. See TextEffects.
font: FontBound font. Assigning triggers re-layout on the next frame.
readonly height: floatMeasured text height in pixels, valid after the first layout pass.
text: stringThe text content. Assigning triggers re-layout on the next frame.
type: TextFieldTypeDYNAMIC (default) or INPUT. Only focused INPUT text can receive typing and end-entry Backspace. For caret, selection or clipboard editing, use willowui's TextInput or TextArea.
maxChars: intMaximum Unicode characters accepted by typing. 0 (default) means unlimited.
restrict: RegExp | nullOptional regex every typed character must match before onTextInput fires.
readonly width: floatMeasured text width in pixels, valid after the first layout pass.

Text: methods ​

MemberDescription
setColor(color: Color): voidEquivalent to label.color = color. Glyph fill colour.
setFont(font: Font): voidReplaces the label's font. Triggers re-layout on the next frame.
setText(text: string): voidEquivalent to label.text = text. Triggers re-layout on the next frame.
onTextInput(cb: (e: TextInputEvent) => void): FunctionRegisters a cancelable pre-append text callback. Returns an unsubscribe function.
onTextComposition(cb: (e: TextCompositionEvent) => void): FunctionRegisters a callback for IME pre-edit updates. They never change text.
onChange(cb: () => void): FunctionRegisters a callback after focused typing or Backspace changes text. Returns an unsubscribe function.
measure(): SizeComputes the text layout (applying any pending property writes) without rendering and returns its { width, height } in pixels.

TextEffects ​

ts
class TextEffects

Outline / glow / drop-shadow parameters for a Text, reached via label.effects. All widths/offsets default to 0 (no effect drawn); colours are the built-in Color, like Text.color. Assigning any field takes effect on the next frame (or immediately before a label.measure() call).

TextEffects: properties ​

MemberDescription
outlineWidth: floatOutline thickness in pixels. 0 disables the outline.
outlineColor: ColorOutline colour.
glowWidth: floatGlow radius in pixels. 0 disables the glow.
glowColor: ColorGlow colour.
shadowOffsetX: floatDrop-shadow horizontal offset in pixels.
shadowOffsetY: floatDrop-shadow vertical offset in pixels.
shadowColor: ColorDrop-shadow colour.

TextFieldType ​

ts
enum TextFieldType

Input mode for a Text node. Dynamic text never receives IME input.

TextFieldType: values ​

MemberDescription
DYNAMICDisplay-only text that does not accept focused typing.
INPUTEditable text that receives IME and character input while focused.

TextInputEvent ​

ts
class TextInputEvent

Cancelable event sent to a focused input Text before its content changes.

TextInputEvent: properties ​

MemberDescription
readonly text: stringAccepted text proposed for insertion.

TextInputEvent: methods ​

MemberDescription
preventDefault(): voidCancels insertion and suppresses the following change callback.

TextCompositionEvent ​

ts
class TextCompositionEvent

A transient IME pre-edit update for a focused input Text.

TextCompositionEvent: properties ​

MemberDescription
readonly text: stringPre-edit text. Empty text with cancelled clears the composition.
readonly selectionStart: intSelected segment start and length, measured in Unicode code points.
readonly selectionLength: intNumber of composed code points selected by the input method.
readonly replacementStart: intRange in the widget selection which the eventual commit replaces. -1 means unspecified.
readonly replacementLength: intNumber of existing code points replaced by this composition.
readonly cancelled: booleanTrue when the platform cancelled the active pre-edit.