Skip to content
rizuki edited this page Nov 30, 2026 · 8 revisions

Text

Text adds a leaf element with one text render command.

Text

Text("Hello, world!",
     .textColor = Color(255, 255, 255, 255),
     .fontSize  = 32);

Text(str, ...) accepts a NUL-terminated const char *. CC_StrIntern() copies its bytes into a process-lifetime pool and gives Clay a stable pointer with .isStaticallyAllocated = true.

  • Caller buffers can expire after the call because interning copies the bytes.
  • Repeated content reuses the same interned pointer.
  • Dynamic strings work with snprintf, slices, or UTF-8 buffers:
char buf[64];
snprintf(buf, sizeof buf, "FPS: %d", fps);
Text(buf, .textColor = Color(200,200,200,255), .fontSize = 12);

Each unique string remains in the intern pool for the process lifetime. Avoid passing large amounts of new text every frame.

Style fields

Arguments map directly to Clay_TextElementConfig:

Field Notes
.textColor Clay_Color, 0 to 255 per channel
.fontId slot id from CC_LoadFont (default 0 = raylib default font, or whatever CC_InitFont set)
.fontSize integer pixel size (default: whatever CC_InitFont set, initially 16)
.letterSpacing extra px between glyphs
.lineHeight px. 0 means "use the font's natural line height"
.wrapMode CC_TEXT_WRAP_WORDS (default), CC_TEXT_WRAP_NEWLINES, CC_TEXT_WRAP_NONE
.textAlignment CC_TEXT_ALIGN_LEFT (default), CC_TEXT_ALIGN_CENTER, CC_TEXT_ALIGN_RIGHT
.userData arbitrary void* forwarded to every text render command

Fonts

Slot 0 is always the raylib default font. To use a custom font, call CC_LoadFont() after CC_Init() and pass the returned id as .fontId:

int FONT_TITLE = CC_LoadFont("resources/Roboto-Bold.ttf", 42);
if (FONT_TITLE < 0) FONT_TITLE = 0;   // graceful fallback

Text("Dashboard",
     .fontId    = FONT_TITLE,
     .fontSize  = 42,
     .textColor = Color(255,255,255,255));

Or promote a loaded font to the project-wide default with CC_InitFont():

int FONT_BODY = CC_LoadFont("resources/Inter-Regular.ttf", 28);
CC_InitFont(FONT_BODY, 16);   // global font id + global default size

// Uses the global font and size: no .fontId, no .fontSize needed
Text("Status", .textColor = Color(220,220,220,255));

// Explicit override
int FONT_MONO = CC_LoadFont("resources/RobotoMono-Medium.ttf", 14);
Text("code", .fontId = FONT_MONO, .fontSize = 14,
             .textColor = Color(180,180,180,255));

CC_LoadFont's base_size controls glyph atlas rasterization. Raise it for text that renders large. CC_InitFont's base_size controls the default rendered size when .fontSize is omitted.

Font-id precedence inside Text

  1. A non-zero .fontId selects that font.
  2. Otherwise, Text uses CC_GetGlobalFontId().
  3. If no global font is set, it uses slot 0, raylib's default font.

This means .fontId = 0 is treated the same as omitting .fontId entirely.

Default font size

An omitted or zero .fontSize uses CC_GetGlobalFontSize(). Its initial value is CC_DEFAULT_FONT_SIZE (16), defined in include/ccompose.h. Change the default with CC_InitFont, or set .fontSize on an individual call.

Wrapping

.wrapMode controls how Clay lays out multi-line text:

Text("This is a long block of text that will wrap at word boundaries...",
     .wrapMode  = CC_TEXT_WRAP_WORDS,
     .textColor = Color(220,220,220,255),
     .fontSize  = 14);
  • CC_TEXT_WRAP_WORDS (default): break at word boundaries, respecting explicit \n.
  • CC_TEXT_WRAP_NEWLINES: only break at \n, never within a word.
  • CC_TEXT_WRAP_NONE: single line. Overflow is handled by the container's .clip.

Give the parent a bounded width for text to wrap. A Grow() width can inherit that constraint from its parent. A Fit() parent has no width constraint to wrap against.

Alignment

.textAlignment controls the alignment of wrapped lines within the text block's own bounding box:

  • CC_TEXT_ALIGN_LEFT (default): lines start at the left edge.
  • CC_TEXT_ALIGN_CENTER: each line is centered.
  • CC_TEXT_ALIGN_RIGHT: lines end at the right edge.
Text("Line one\nA longer line two",
     .textAlignment = CC_TEXT_ALIGN_CENTER,
     .textColor     = Color(220,220,220,255),
     .fontSize      = 16);

The effect is only visible when the block has lines of different lengths, from wrapping or from explicit \n. To center a text leaf inside its parent container, use the parent's .layout.childAlignment instead:

Column("Banner",
       .layout = { .sizing = { Grow(), Fit() },
                   .childAlignment = { .x = AlignXCenter() } }) {
    Text("centered by parent alignment",
         .textColor = Color(255,255,255,255),
         .fontSize  = 20);
}

Colors

Text color comes from a global default that you can override per-call.

Set the project-wide default once with CC_SetGlobalFontColor() (defaults to ColorHex(0xFFFFFF)):

CC_SetGlobalFontColor(ColorHex(0xDCDCDC));   // global default

Text("Status", .fontSize = 14);                            // uses global (220,220,220,255)
Text("error",  .fontSize = 14, .textColor = Color(230, 80, 80, 255));   // per-call override

Per-call .textColor wins over the global. See Lifecycle for the global color API.

Performance notes

  • Clay caches measurements by (chars-pointer, length, font, size). CC_StrIntern reuses the pointer for matching content, so a formatted "FPS: 60" can share a cache entry with the same literal. New content requires a fresh measurement.
  • For CLAY_ERROR_TYPE_TEXT_MEASUREMENT_CAPACITY_EXCEEDED, increase the cache with Clay_SetMaxMeasureTextCacheWordCount(n) after CC_Init().

Clone this wiki locally