# Draw and measure text

Place a label on its alphabetic baseline and show the measured area behind it. Tap to change the font size; the measured width and font height update with the text.

Text coordinates are not automatically the top-left corner of the visible letters. Alignment and baseline settings determine how the drawing origin relates to the text.

<div class="docs-demo-layout">

```csharp
using Xui.Core.Canvas;
using Xui.Core.Math2D;
using Xui.Core.UI;
using Xui.Core.UI.Input;
using NFloat = System.Runtime.InteropServices.NFloat;

public sealed class TextMeasureView : View
{
    private bool large;
    protected override void RenderCore(IContext context)
    {
        context.SetFill(new Color(0xFAF7FFFF));
        context.FillRect(Frame);
        context.SetFont(new Font(large ? 42 : 28, "Inter"));
        const string text = "Hello, Xui!";
        var metrics = context.MeasureText(text);
        NFloat x = Frame.Center.X - metrics.Size.Width / 2;
        NFloat baseline = Frame.Center.Y;
        context.SetFill(new Color(0xEADCF5FF));
        context.FillRect(new Rect(x, baseline - metrics.Font.FontBoundingBoxAscent, metrics.Size.Width, metrics.Font.Height));
        context.SetStroke(new Color(0xD957A8FF));
        context.LineWidth = 1;
        context.BeginPath();
        context.MoveTo((Frame.X + 24, baseline));
        context.LineTo((Frame.Right - 24, baseline));
        context.Stroke();
        context.TextAlign = TextAlign.Left;
        context.TextBaseline = TextBaseline.Alphabetic;
        context.SetFill(new Color(0x492571FF));
        context.FillText(text, (x, baseline));
        context.SetFont(new Font(17, "Inter"));
        context.TextAlign = TextAlign.Center;
        context.FillText("Pink line: alphabetic baseline", (Frame.Center.X, baseline + 72));
        context.FillText("Tap to change the font size", (Frame.Center.X, Frame.Y + 56));
    }

    public override void OnPointerEvent(ref PointerEventRef e, EventPhase phase)
    {
        if (phase == EventPhase.Bubble && e.Type == PointerEventType.Down)
        {
            large = !large;
            InvalidateRender();
        }
    }
}
```

<figure class="docs-demo" data-demo-url="../../../wasm/?embed=1&amp;demo=canvas/text-metrics">
<div class="docs-demo-stage">
<img src="canvas-text-metrics.svg" width="400" height="600" alt="Text metrics: initial rendered view" />
<button type="button" class="docs-demo-start" hidden><span class="docs-demo-pill">Start demo</span></button>
</div>
<div class="docs-demo-actions">
<button type="button" class="docs-demo-close" hidden>Close demo</button>
<span class="docs-demo-status" role="status" aria-live="polite"></span>
</div>
<figcaption>Text metrics · <a href="canvas-text-metrics.cs">C# source</a> · <a href="canvas-text-metrics.svg">SVG</a> · <a href="../../../wasm/?embed=1&amp;demo=canvas/text-metrics">Open demo</a></figcaption>
</figure>

</div>


## Measure with the font you will draw

Call `SetFont` before `MeasureText`. The returned metrics depend on the font family, size, weight, and the text. Then draw with that same font. Measuring at one size and drawing at another makes centering and hit testing wrong.

`metrics.Size.Width` is the advance width: the distance the text consumes along the line. The example subtracts half that width from the frame's center to center a left-aligned line. It sets `TextAlign.Left` explicitly so no previous alignment changes that calculation.

The pink line is the alphabetic baseline. Ascenders extend above it and descenders extend below it. The tinted rectangle uses `FontBoundingBoxAscent` and `Font.Height`; it describes a font-based line area, not the exact ink outline of these particular characters.

Use the line-specific metrics when your job depends on the actual glyph bounds. Advance width, font line height, and visible ink bounds answer different questions; avoid treating them as interchangeable rectangles.

## Choose the alignment for the job

For a centered button caption, `TextAlign.Center` with `TextBaseline.Middle` is often the direct expression of the intent. For a row of labels that must share a reading baseline, use `Alphabetic` and the same baseline Y coordinate.

Keep font loading consistent across measurement and rendering. The examples use bundled Inter. If the requested font is missing and a runtime substitutes another face, text can change width and line height. Test your actual assets and target platforms rather than relying on a font installed only on your development machine.

## Fit available width deliberately

`FillText` draws a line; it is not a paragraph layout engine. For ordinary page text, prefer `Label` and the surrounding layout. For custom Canvas labels, measure first and choose an explicit policy: wrap at valid text boundaries, elide, clip, or reduce size within a readable limit.

Do not cut arbitrary UTF-16 indices to implement wrapping or editing. A visible character can contain multiple code units, combining marks, or joined emoji. Text measurement alone does not provide the full editing model.

For a changing caption, update the state outside `RenderCore`. Invalidate measure if the caption affects the desired size, and invalidate rendering when the drawn result changes. A fixed-size instrument that only changes its text can often keep its layout unchanged.

## Format changing labels outside drawing

Keep a string field for a changing label and update it when its displayed value changes. Pass that string to `FillText` during rendering. This avoids formatting on every frame and works around the current browser runtime issue with the direct Canvas interpolated-string overload. The input [drag example](../../input/gestures/index.md) shows this pattern.
