Search Docs
Loading...
Skip to content

Adjust Text Spacing

Control letter spacing, line height, and paragraph spacing in text blocks using the Block API.

CE.SDK provides three text spacing properties — text/letterSpacing, text/lineHeight, and text/paragraphSpacing — controlled via engine.block.setFloat() and engine.block.getFloat(). In addition, engine.block.setTextLineHeight() and engine.block.getTextLineHeight() let you set per-paragraph line height overrides on top of the block-level value.

Letter Spacing#

Control the horizontal space between characters with the text/letterSpacing property. Positive values spread characters apart; negative values tighten them.

// Set letter spacing — positive values spread characters, negative values tighten them
engine.block.setFloat(text, property = "text/letterSpacing", value = 0.1F)
// Read the current letter spacing
engine.block.getFloat(text, property = "text/letterSpacing")

Letter spacing (also called tracking) adjusts text density for improved readability or visual effect.

Line Height#

Control the vertical distance between lines with the text/lineHeight property. The value is a multiplier of the font size — for example, 1.5 means 150% of the font size.

// Set the block-level line height multiplier — applies to all paragraphs by default
engine.block.setFloat(text, property = "text/lineHeight", value = 1.5F)
// Read the current block-level line height
engine.block.getFloat(text, property = "text/lineHeight")

This block-level value applies to all paragraphs unless overridden at the paragraph level.

Per-Paragraph Line Height#

Override the line height for individual paragraphs using engine.block.setTextLineHeight() with a paragraphIndex. Passing null for the lineHeight clears the override and reverts that paragraph to the block-level value. Calling setTextLineHeight() without a paragraphIndex (or with -1) sets the block-level line height and clears all paragraph overrides at once.

engine.block.getTextLineHeight() returns the effective line height for a given paragraph — the per-paragraph override if one is set, otherwise the block-level value.

// Set a per-paragraph line height override for paragraph 0 (first paragraph)
engine.block.setTextLineHeight(text, lineHeight = 2.0F, paragraphIndex = 0)
// Read the line height for a specific paragraph
// Returns the override if set, otherwise falls back to the block-level value
engine.block.getTextLineHeight(text, paragraphIndex = 0)
engine.block.getTextLineHeight(text, paragraphIndex = 1)
// Clear a paragraph's override by passing null — it reverts to the block-level value
engine.block.setTextLineHeight(text, lineHeight = null, paragraphIndex = 0)
// Set the block-level line height and clear all paragraph overrides at once
engine.block.setTextLineHeight(text, lineHeight = 1.8F)
  • para0LineHeight returns 2.0 (the override).
  • para1LineHeight returns the block-level value because paragraph 1 has no override.
  • After the final setTextLineHeight(text, lineHeight = 1.8F) call, all paragraph overrides are cleared and the block-level value becomes 1.8.

Paragraph Spacing#

Add vertical space between paragraphs using the text/paragraphSpacing property. The value is added after each paragraph break (newline character).

// Set paragraph spacing — adds space after each paragraph break
engine.block.setFloat(text, property = "text/paragraphSpacing", value = 20F)
// Read the current paragraph spacing
engine.block.getFloat(text, property = "text/paragraphSpacing")

Paragraph spacing only affects text with actual paragraph breaks. Single-paragraph text won’t show a visible difference.

Full Code#

Here’s the full code:

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import ly.img.engine.DesignBlockType
import ly.img.engine.Engine
import ly.img.engine.SizeMode
fun textAdjustSpacing(
license: String,
userId: String,
) = CoroutineScope(Dispatchers.Main).launch {
val engine = Engine.getInstance(id = "ly.img.engine.example")
engine.start(license = license, userId = userId)
engine.bindOffscreen(width = 100, height = 100)
val scene = engine.scene.create()
val text = engine.block.create(DesignBlockType.Text)
engine.block.appendChild(parent = scene, child = text)
engine.block.setWidthMode(text, mode = SizeMode.AUTO)
engine.block.setHeightMode(text, mode = SizeMode.AUTO)
engine.block.replaceText(text, text = "Hello\nWorld\nCE.SDK")
// Set letter spacing — positive values spread characters, negative values tighten them
engine.block.setFloat(text, property = "text/letterSpacing", value = 0.1F)
// Read the current letter spacing
engine.block.getFloat(text, property = "text/letterSpacing")
// Set the block-level line height multiplier — applies to all paragraphs by default
engine.block.setFloat(text, property = "text/lineHeight", value = 1.5F)
// Read the current block-level line height
engine.block.getFloat(text, property = "text/lineHeight")
// Set a per-paragraph line height override for paragraph 0 (first paragraph)
engine.block.setTextLineHeight(text, lineHeight = 2.0F, paragraphIndex = 0)
// Read the line height for a specific paragraph
// Returns the override if set, otherwise falls back to the block-level value
engine.block.getTextLineHeight(text, paragraphIndex = 0)
engine.block.getTextLineHeight(text, paragraphIndex = 1)
// Clear a paragraph's override by passing null — it reverts to the block-level value
engine.block.setTextLineHeight(text, lineHeight = null, paragraphIndex = 0)
// Set the block-level line height and clear all paragraph overrides at once
engine.block.setTextLineHeight(text, lineHeight = 1.8F)
// Set paragraph spacing — adds space after each paragraph break
engine.block.setFloat(text, property = "text/paragraphSpacing", value = 20F)
// Read the current paragraph spacing
engine.block.getFloat(text, property = "text/paragraphSpacing")
engine.stop()
}