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- Pick a release from britzl/defold-richtext releases and add its Source code (zip) URL (or a packaged
.zipasset, if the library ships one) togame.projectunder[project]dependencies, then Fetch Libraries in the Defold editor. - Run
bunx @defold-typescript/cli resolveto materialize its types. - Import it under a namespace alias of your choice:
import * as richtext from "richtext.richtext"
- Pick a release from britzl/defold-richtext releases and add its Source code (zip) URL (or a packaged
Functions
create(text: string, font: string, settings?: Settings): LuaMultiReturn<[ Word[], TextMetrics ]>tagged(words: Word[], tag: string): Word[]truncate(words: Word[], length: number, options?: { words: boolean; }): Wordlength(text: Word[] | string): numbercharacters(word: Word): Word[]on_click(words: Word[], action: { node_id: Hash; text: string; screen_x: number; screen_y: number; tags: string[]; }): booleanremove(words: Word[])plaintext(words: Word[]): string
create(text: string, font: string, settings?: Settings): LuaMultiReturn<[ Word[], TextMetrics ]>
Creates rich text gui nodes from a text containing markup.
Parameters
text:stringfont:stringsettings?: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:
- It can truncate the text on a per word basis or
- 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:numberoptions?:{ 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:Hashtext:stringscreen_x:numberscreen_y:numbertags: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