Skip to main content

Reading and modifying text properties in the Headless CreativeEngine

In this example, we want to show how to read and modify the text block's contents via the API in the Headless CreativeEngine.

Explore a full code sample of the integration on CodeSandbox or view the code on GitHub.

Editing the Text String#

You can edit the text string contents of a text block using the replaceText(id: DesignBlockId, text: string, from: number = -1, to: number = -1) and removeText(id: DesignBlockId, from: number = -1, to: number = -1) APIs. The range of text that should be edited is defined using the UTF-16 indices [from, to).

When omitting both the from and to arguments, the entire existing string is replaced.

When only specifying the from index, the new text is inserted at this index.

When both from and to indices are specified, then that range of text is replaced with the new text.

Similarly, the removeText API can be called to remove either a specific range or the entire text.

Text Colors#

Text blocks in the CreativeEngine allow different ranges to have multiple colors.

Use the setTextColor(id: DesignBlockId, color: RGBAColor | SpotColor, from: number = -1, to: number = -1) API to change either the color of the entire text

or only that of a range. After these two calls, the text "Alex!" now starts with one yellow character, followed by three black characters and two more yellow ones.

The getTextColors(id: DesignBlockId, from: number = -1, to: number = -1): Array<RGBAColor | SpotColor> API returns an ordered list of unique colors in the requested range. Here, allColors will be an array containing the colors yellow and black (in this order).

When only the colors in the UTF-16 range from 2 to 5 are requested, the result will be an array containing black and then yellow, since black appears first in the requested range.

Font Weights and Styles#

Text blocks can also have multiple ranges with different weights and styles. These can currently only be edited by the user directly on the canvas but APIs for changing these values programmatically will be added in the future. The current APIs allow you to query the current weights and styles in the text block.

The getTextFontWeights(id: DesignBlockId, from: number = -1, to: number = -1): FontWeight[] API returns an ordered list of unique font weights in the requested range, similar to the getTextColors API described above. For this example text, the result will be ['normal'].

The getTextFontStyles(id: DesignBlockId, from: number = -1, to: number = -1): FontStyle[] API returns an ordered list of unique font styles in the requested range, similar to the getTextColors API described above. For this example text, the result will be ['normal'].

Text Case#

You can apply text case modifications to ranges of text in order to display them in upper case, lower case or title case. It is important to note that these modifiers do not change the text string value of the text block but are only applied when the block is rendered.

By default, the text case of all text within a text block is set to Normal, which does not modify the appearance of the text at all.

The setTextCase(id: DesignBlockId, textCase: TextCase, from: number = -1, to: number = -1): void API sets the given text case for the selected range of text.

Possible values for TextCase are:

  • Normal: The text string is rendered without modifications.
  • Uppercase: All characters are rendered in upper case.
  • Lowercase: All characters are rendered in lower case.
  • Titlecase: The first character of each word is rendered in upper case.

The getTextCases(id: DesignBlockId, from: number = -1, to: number = -1): TextCase[] API returns the ordered list of text cases of the text in the selected range.

import CreativeEngine from '';
const config = {
baseURL: ''
async (engine) => {
// Add default assets used in scene.
const scene = engine.scene.create();
const text = engine.block.create('text');
engine.block.appendChild(scene, text);
engine.block.setWidthMode(text, 'Auto');
engine.block.setHeightMode(text, 'Auto');
engine.block.replaceText(text, 'Hello World');
// Add a "!" at the end of the text
engine.block.replaceText(text, "!", 11);
// Replace "World" with "Alex"
engine.block.replaceText(text, "Alex", 6, 11);
engine.scene.zoomToBlock(text, 100, 100, 100, 100);
// Remove the "Hello "
engine.block.removeText(text, 0, 6)
engine.block.setTextColor(text, { r: 1.0, g: 1.0, b: 0.0, a: 1.0 });
engine.block.setTextColor(text, { r: 0.0, g: 0.0, b: 0.0, a: 1.0 }, 1, 4);
const allColors = engine.block.getTextColors(text);
const colorsInRange = engine.block.getTextColors(text, 2, 5);
const fontWeights = engine.block.getTextFontWeights(text);
const fontStyles = engine.block.getTextFontStyles(text);
engine.block.setTextCase(text, 'Titlecase');
const textCases = engine.block.getTextCases(text);