On this page

britzl/defold-richtext/richtext.richtext

Rich-text rendering for Defold: styled spans, embedded tags, and per-color text runs.

  • GitHub: britzl/defold-richtext — pinned to 2fe3aed
    1. Pick a release from britzl/defold-richtext releases and add its Source code (zip) URL (or a packaged .zip asset, if the library ships one) to game.project under [project] dependencies, then Fetch Libraries in the Defold editor.
    2. Run bunx @defold-typescript/cli resolve to materialize its types.
    3. Import it under a namespace alias of your choice:
      import * as richtext from "richtext.richtext"

Functions

create(text: string, font: string, settings?: Settings): LuaMultiReturn<[ Word[], TextMetrics ]>

Creates rich text gui nodes from a text containing markup.

Parameters

  • text: string
  • font: string
  • settings?: Settings

Returns

  • LuaMultiReturn<[ Word[], TextMetrics ]>

tagged(words: Word[], tag: string): Word[]

Get all words with a specific tag.

Parameters

  • words: Word[]
  • tag: string

Returns

  • Word[]

truncate(words: Word[], length: number, options?: { words: boolean; }): Word

Truncate a text down to a specific length. This function has two modes of operation:

  1. It can truncate the text on a per word basis or
  2. on a per character/image basis. The function will disable nodes that shouldn't be visible and in the case of truncating on a per character basis also update the text in nodes that should be partially visible. The text metrics of a truncated word will be updated.

Parameters

  • words: Word[]
  • length: number
  • options?: { words: boolean; }
    • words: boolean

Returns

  • Word

length(text: Word[] | string): number

Get the length of a text ignoring any tags except image and spine tags which are treated as having a length of 1.

Parameters

  • text: Word[] | string

Returns

  • number

characters(word: Word): Word[]

Split a word into it's characters, including the creation of the gui nodes. Each of the characters will be given the same attributes as the word, and they will be positioned correctly within the word.

Parameters

  • word: Word

Returns

  • Word[]

on_click(words: Word[], action: { node_id: Hash; text: string; screen_x: number; screen_y: number; tags: string[]; }): boolean

Call this function when a click/touch has been detected and your text contains words with an a tag. These words act as "hyperlinks" and will generate a message when clicked.

Parameters

  • words: Word[]
  • action: { node_id: Hash; text: string; screen_x: number; screen_y: number; tags: string[]; }
    • node_id: Hash
    • text: string
    • screen_x: number
    • screen_y: number
    • tags: string[]

Returns

  • boolean

remove(words: Word[])

Removes all gui text nodes created by richtext.create().

Parameters

  • words: Word[]

plaintext(words: Word[]): string

Returns the words created by richtext.create() as a plain text string without any formatting or tags. Linebreaks are included in the returned string.

Parameters

  • words: Word[]

Returns

  • string

Variables

ALIGN_LEFT: Alignment

ALIGN_CENTER: Alignment

ALIGN_RIGHT: Alignment

ALIGN_JUSTIFY: Alignment

VALIGN_TOP: VAlignment

VALIGN_MIDDLE: VAlignment

VALIGN_BOTTOM: VAlignment