DrawFont:GetTextBounds Synchronous

Estimate the space a text string needs at a requested font size. Use the estimate to reserve space for a label before drawing it. Leave margin for the actual font because this implementation does not measure individual glyphs.

Syntax
Luau
DrawFont:GetTextBounds(size: number, text: string) -> Vector2

Parameters

Call this method with a DrawFont receiver. The colon supplies self implicitly; a dot call must pass the receiver explicitly.

Function parameters
ParameterTypeDescription
sizenumberNon-negative font size in pixels.
textstringString whose approximate bounds are needed.

Returns

Vector2

A Vector2 containing estimated width and height. An empty string returns zero height.

Usage notes

The current implementation estimates width from the character count and font size. It does not measure the registered font’s glyphs, kerning, or line breaks; allow extra space when laying out text.

Differences from Volt

Volt presents this as font measurement. Kawaii currently estimates width from character count and requested size, with a size-based height; it does not measure glyph shapes, kerning, or multiline layout.

Example

Example
Luau
-- Put a valid font file at this workspace path first.
local font = DrawFont.Register(readfile("fonts/example.ttf"), { PixelSize = 18 })
if font then
    local bounds = font:GetTextBounds(18, "Preview")
    print("Estimated width:", bounds.X, "height:", bounds.Y)
end
Kawaii documentation