Keel v0.5.1 中文 GitHub

Label

Live example: a real Keel application compiled to WebAssembly. Interact with it here. The initial download is about 10 MB.Open full screen
Example sourceGo · Copyable

This is the complete source for the gallery example above.examples/components/label.go

Reuse the component construction and Render methods in a scaffolded project. The example’s registerSection and shared helpers are provided by the component gallery.

package main

import (
	"github.com/dyike/keel/ui/core"
	"github.com/dyike/keel/ui/el"
	"github.com/dyike/keel/ui/kit"
)

func init() {
	registerSection("label", "general", func() core.Widget {
		masked := kit.Label(demoText("Account balance ¥12,345", "账户余额 ¥12,345")).Masked(true).Secondary(demoText("(masks the primary text only)", "(仅遮罩主文案)"))
		hidden := true
		return el.Embed(el.ViewFunc(func(cx *el.Context) el.Element {
			return el.Div().P(24).Gap(20).Child(
				kit.Label(demoText("Company name", "公司名称")).Secondary(demoText("(optional)", "(可选)")).Render(cx),
				kit.Label(demoText("Hello world", "你好,世界")).Highlights("Hello").Style(func(t *el.TextEl) { t.TextSize(24).Bold() }).Render(cx),
				kit.Label(demoText("Prefix matching; highlights the prefix only", "前缀匹配,只高亮前缀")).HighlightPrefix(demoText("Prefix", "前缀")).Render(cx),
				kit.Label(demoText("This text wraps to the container width. Highlighted matches retain their layout even across line breaks.", "这是一段可以自动换行的文案,匹配文字跨行仍保留完整排版。文案会按照容器宽度布局。")).Highlights(demoText("Text", "文案")).Render(cx),
				masked.Render(cx), kit.Button(demoText("Show / mask", "显示 / 遮罩"), func() { hidden = !hidden; masked.Masked(hidden) }).Render(cx),
			)
		}))
	})
}

kit.Label(text) displays wrappable labels. Secondary(text) appends the secondary color copy after the same text stream and removes the empty string; SetText updates the main copy.

kit.Label("Company name").Secondary("(optional)").Highlights("Company")
kit.Label("Account balance").Masked(true)
kit.Label("Hello World").HighlightPrefix("Hello").Style(func(t *el.TextEl) {
    t.TextSize(20).Bold().LineHeight(1.5)
})

Highlights is case-sensitive and marks all non-overlapping exact matches; HighlightPrefix only matches the beginning, and the call after the two will take effect, and the empty string will be cleared. HighlightColor Overrides the theme PrimaryText. The secondary copywriter uses the current theme Muted and does not participate in the search.

Masked(true) replaces each Unicode rune of the main copy with a dot and turns off the main copy highlighting; both rendering and Agent semantics only contain dots. Secondary copy is still visible. Combining characters and emoji sequences may correspond to multiple dots and are not counted by glyphs.

Style receives a new TextEl for each frame, configurable font, weight, line height, alignment, width, MaxLines and FocusOnPress field focus associations. Do not retain a reference to the element or re-expose the masked text via a custom Name. nil restores the default style.

The underlying el.Text(...).Ranges(...) is colored with half-open rune intervals and does not split the entire section for typesetting. When matching involves ligatures/combining characters, the entire glyph cluster is colored; colored bitmap glyphs remain in their original color, and truncated ellipses use the base color. The latter takes precedence over overlapping intervals, and Shimmer takes precedence over Ranges.