primitives/text.go

Functions Structs

Functions

func AccessibilityActions

AccessibilityActions returns nil. Text has no actions.

func (t *TextWidget) AccessibilityActions() []a11y.Action {
	return nil
}

func AccessibilityHint

AccessibilityHint returns an empty string.

func (t *TextWidget) AccessibilityHint() string {
	return ""
}

func AccessibilityLabel

AccessibilityLabel returns the text content.

func (t *TextWidget) AccessibilityLabel() string {
	return t.Content()
}

func AccessibilityRole

AccessibilityRole returns [a11y.RoleLabel].

func (t *TextWidget) AccessibilityRole() a11y.Role {
	return a11y.RoleLabel
}

func AccessibilityState

AccessibilityState returns the default state.

func (t *TextWidget) AccessibilityState() a11y.State {
	return a11y.State{
		Hidden: !t.IsVisible(),
	}
}

func AccessibilityValue

AccessibilityValue returns an empty string.

func (t *TextWidget) AccessibilityValue() string {
	return ""
}

func Align

Align sets horizontal text alignment.

func (t *TextWidget) Align(a TextAlign) *TextWidget {
	t.style.Align = a
	return t
}

func Bold

Bold enables bold font weight.

func (t *TextWidget) Bold() *TextWidget {
	t.style.Bold = true
	return t
}

func Children

Children returns nil. Text is a leaf widget.

func (t *TextWidget) Children() []widget.Widget {
	return nil
}

func Color

Color sets the text color explicitly.

 

An explicit color always takes precedence over the theme's default

OnSurface color.

func (t *TextWidget) Color(c widget.Color) *TextWidget {
	t.style.Color = c
	t.colorExplicit = true
	return t
}

func Content

Content returns the current text content.

 

Resolution priority: [ContentSignal] > [TextFn] > static [Text].

func (t *TextWidget) Content() string {
	if t.contentSignal != nil {
		return t.contentSignal.Get()
	}
	if t.fn != nil {
		return t.fn()
	}
	return t.content
}

func ContentSignal

ContentSignal binds the text content to a read-only reactive signal.

When set, the signal value takes precedence over both [TextFn] and static

content set via [Text].

 

Because TextWidget is display-only, only [state.ReadonlySignal] is accepted

(no write-back capability is needed).

 

name := state.NewSignal("Alice")

label := primitives.Text("").ContentSignal(name).FontSize(14)

func (t *TextWidget) ContentSignal(sig state.ReadonlySignal[string]) *TextWidget {
	t.contentSignal = sig
	return t
}

func Draw

Draw renders the text content.

 

The text color is resolved with the following priority:

1. Explicit color set via [TextWidget.Color] (always wins)

2. ThemeProvider's OnSurface color (if a theme is active)

3. [widget.ColorBlack] (fallback when no theme is set)

 

When a custom [TextWidget.FontFamily] or [TextWidget.Italic] is set

and the canvas supports [widget.StyledTextDrawer], the styled text

path is used to render with the custom font. Otherwise, the standard

[widget.Canvas.DrawText] is used with the default embedded font.

func (t *TextWidget) Draw(ctx widget.Context, canvas widget.Canvas) {
	if !t.IsVisible() {
		return
	}

	text := t.Content()
	if text == "" {
		return
	}

	bounds := t.Bounds()
	if bounds.IsEmpty() {
		return
	}

	color := t.resolveColor(ctx)

	// Use StyledTextDrawer path when custom font family or italic is set.
	if t.style.FontFamily != "" || t.style.Italic {
		if sd, ok := canvas.(widget.StyledTextDrawer); ok {
			sd.DrawStyledText(text, bounds, widget.TextStyle{
				FontFamily:	t.style.FontFamily,
				FontSize:	t.style.FontSize,
				Bold:		t.style.Bold,
				Italic:		t.style.Italic,
				Color:		color,
				Align:		t.style.Align,
			})
			return
		}
	}

	canvas.DrawText(text, bounds, t.style.FontSize, color, t.style.Bold, t.style.Align)
}

func Ellipsis

Ellipsis enables truncation with "..." when text overflows.

func (t *TextWidget) Ellipsis() *TextWidget {
	t.style.Overflow = TextOverflowEllipsis
	return t
}

func Event

Event returns false. Text widgets do not consume events.

func (t *TextWidget) Event(_ widget.Context, _ event.Event) bool {
	return false
}

func FontFamily

FontFamily sets a custom font family name.

 

The font must be registered via the plugin system's [plugin.AssetLoader]

or directly via the font registry before it can be used. When set, the

widget will use [widget.StyledTextDrawer] to render text with the custom

font, falling back to the standard [widget.Canvas.DrawText] with Inter

if the canvas does not support styled text.

 

Example:

 

label := primitives.Text("CJK").FontFamily("NotoSansCJK").FontSize(18)

func (t *TextWidget) FontFamily(name string) *TextWidget {
	t.style.FontFamily = name
	return t
}

func FontSize

FontSize sets the font size in logical pixels.

func (t *TextWidget) FontSize(size float32) *TextWidget {
	t.style.FontSize = size
	return t
}

func IsReactive

IsReactive returns true if the widget uses a function or signal for its content.

func (t *TextWidget) IsReactive() bool {
	return t.contentSignal != nil || t.fn != nil
}

func Italic

Italic enables italic font style.

func (t *TextWidget) Italic() *TextWidget {
	t.style.Italic = true
	return t
}

func Layout

Layout measures the text and returns the constrained size.

 

Text measurement uses a simple character-width heuristic. Real font

measurement is delegated to the Canvas implementation in production.

func (t *TextWidget) Layout(_ widget.Context, constraints geometry.Constraints) geometry.Size {
	text := t.Content()
	size := t.measureText(text, constraints.MaxWidth)
	resultSize := constraints.Constrain(size)
	t.SetBounds(geometry.FromPointSize(t.Position(), resultSize))
	return resultSize
}

func LineHeight

LineHeight sets the line height multiplier. The default is 1.2.

func (t *TextWidget) LineHeight(v float32) *TextWidget {
	t.style.LineHeight = v
	return t
}

func MaxLines

MaxLines limits the number of displayed lines. Zero means unlimited.

func (t *TextWidget) MaxLines(n int) *TextWidget {
	t.style.MaxLines = n
	return t
}

func Mount

Mount creates signal bindings for push-based invalidation.

Implements [widget.Lifecycle].

func (t *TextWidget) Mount(ctx widget.Context) {
	sched := ctx.Scheduler()
	if sched == nil {
		return
	}
	if t.contentSignal != nil {
		b := state.BindToScheduler(t.contentSignal, t, sched)
		t.AddBinding(b)
	}
}

func Style

Style returns the current text style (read-only snapshot).

func (t *TextWidget) Style() TextStyle {
	return t.style
}

func Text

Text creates a new text widget with static content.

 

label := primitives.Text("Hello World").FontSize(18).Bold()

func Text(content string) *TextWidget {
	t := &TextWidget{
		content:	content,
		style: TextStyle{
			FontSize:	defaultFontSize,
			Color:		widget.ColorBlack,
			LineHeight:	defaultLineHeight,
		},
	}
	t.SetVisible(true)
	t.SetEnabled(true)
	return t
}

func TextFn

TextFn creates a new text widget with reactive content.

 

The function fn is called during layout and draw to obtain the current

text. When the function reads a signal's value, changes to that signal

will cause re-layout and re-draw when the binding is set up externally.

 

counter := state.NewSignal(0)

label := primitives.TextFn(func() string {

return fmt.Sprintf("Count: %d", counter.Get())

}).FontSize(14)

func TextFn(fn func() string) *TextWidget {
	t := &TextWidget{
		fn:	fn,
		style: TextStyle{
			FontSize:	defaultFontSize,
			Color:		widget.ColorBlack,
			LineHeight:	defaultLineHeight,
		},
	}
	t.SetVisible(true)
	t.SetEnabled(true)
	return t
}

func Unmount

Unmount is called when the text widget is removed from the widget tree.

Implements [widget.Lifecycle].

func (t *TextWidget) Unmount() {
	// Bindings are cleaned up automatically by WidgetBase.CleanupBindings().
}

Structs

type TextStyle struct

TextStyle holds all visual styling for a [TextWidget].

type TextStyle struct {
	FontSize	float32
	Color		widget.Color
	Bold		bool
	Italic		bool
	Align		TextAlign
	MaxLines	int
	Overflow	TextOverflow
	LineHeight	float32

	// FontFamily specifies a custom font family name (e.g., "NotoSansCJK").
	// When empty (the default), the embedded Inter font is used.
	// Custom fonts must be registered via the plugin system or font registry
	// before they can be referenced here.
	FontFamily	string
}

type TextWidget struct

TextWidget displays static or reactive text content.

 

TextWidget implements [widget.Widget] and [a11y.Accessible].

 

Create a TextWidget with [Text] (static) or [TextFn] (reactive).

 

# Theme-Aware Default Color

 

By default, TextWidget uses the theme's OnSurface color for text

when a ThemeProvider is available on the context. If no theme is set,

the fallback is [widget.ColorBlack]. An explicit call to [TextWidget.Color]

always takes precedence over the theme default.

type TextWidget struct {
	widget.WidgetBase

	style		TextStyle
	content		string
	fn		func() string
	contentSignal	state.ReadonlySignal[string]
	colorExplicit	bool	// true when Color() was called explicitly
}