Skip to content

Typography 排版 ​

提供统一的文本、标题、段落和链接排版层级。

何时使用 ​

  • 页面标题使用 Title,正文使用 Paragraph 或 Text。
  • 需要可点击的文字操作时使用 Link,不要用普通文本模拟链接。
  • 复杂富文本或代码编辑使用专用组件。

在线体验 ​

Typography 排版 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

go
import "github.com/tangoui/tango/ui"

func page(c *ui.Context) {
    ui.Title(c, "项目概览", 1)
    ui.Paragraph(c, "这里是一段用于说明页面内容的正文。")
    ui.Link(c, "查看详情")
}

常见用法 ​

层级标题 ​

go
func headings(c *ui.Context) {
    ui.Title(c, "一级标题", 1)
    ui.Title(c, "二级标题", 2)
    ui.Title(c, "三级标题", 3)
}

文本装饰 ​

go
func decoration(c *ui.Context) {
    ui.Text(c, "重点").Mark()
    ui.Text(c, "代码").Code()
    ui.Text(c, "已删除").Delete().Underline()
}

链接交互 ​

go
func action(c *ui.Context, open *bool) {
    if ui.Link(c, "打开详情").Clicked() { *open = true }
}

主题与样式 ​

Typography 令牌控制字号、字重、行高、标题颜色和链接状态;三套主题只改变令牌配方,标题层级仍为 1–5。Layout 可为文本应用布局选项。

注意事项 ​

标题层级应按页面结构递进;动态文本不要拼接未转义的用户输入。链接提供清晰的动作名,并确保可用 Tab 聚焦、Enter 激活;RTL 下文本方向和对齐跟随作用域。

示例代码 ​

以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。

Typography 排版

gallery 演示 Typography 的 Render 函数,源码 page_general.go#L46。

加载中…

Typography 可复制与可编辑

gallery 演示 TypographyEditable 的 Render 函数,源码 page_data_misc2.go#L35。

加载中…

Typography 链接下划线与图标

gallery 演示 TypographyAlign 的 Render 函数,源码 page_b1a.go#L51。

加载中…

API ​

签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。

入口函数 ​

ui.Text ​

go
func Text(c *Context, s string) *Label

Text adds a single paragraph of text that wraps to its width.

返回 *Label,可链式调用的方法见 Label 方法。

ui.Title ​

go
func Title(c *Context, s string, level int) *Label

Title adds a heading; level 1..5 like Typography.Title.

返回 *Label,可链式调用的方法见 Label 方法。

ui.Paragraph ​

go
func Paragraph(c *Context, s string) *Label

Paragraph is Text intended for long-form content.

返回 *Label,可链式调用的方法见 Label 方法。

go
func Link(c *Context, s string) *LinkW

Link adds a hyperlink-styled text button.

ConfigProvider.Defaults("Link", ...) keys: "underline" ("always" | "hover" | "never"; Element underline).

返回 *LinkW,可链式调用的方法见 LinkW 方法。

Label 方法 ​

ui.Label:Label is a run of text (Typography.Text).

方法说明签名
AlignAlign aligns the text in its box.Align(a TextAlign) *Label
CodeCode() *Label
ColorColor sets the text color.Color(col theme.Color) *Label
CopiedCopied reports whether the copy icon was clicked within the last 3s.Copied() bool
CopyTextCopyText is Copyable with a different clipboard payload.CopyText(payload string) *Label
CopyableCopyable adds a copy icon after the text; clicking it writes the text to the clipboard and shows a check mark for three seconds.Copyable() *Label
DeleteDelete() *Label
DisabledDisabled greys the text out.Disabled() *Label
EditableEditable adds an edit icon; clicking it swaps the text for an input.Editable(val *string) *Label
EditingEditing reports whether the label is being edited.Editing() bool
EllipsisEllipsis keeps one line and truncates with "…".Ellipsis() *Label
ExpandableExpandable adds an "展开" link when the text is cut by Lines(n); open is toggled by the link ("收起" collapses when open).Expandable(open *bool) *Label
IDID returns the node ID.ID() ID
ItalicItalic slants the text (synthetic oblique).Italic() *Label
LayoutLayout applies layout options to the text node.Layout(opts ...layout.Option) *Label
LineHeightLineHeight overrides the line height in px.LineHeight(px float32) *Label
LinesLines limits wrapping to n lines with an ellipsis.Lines(n int) *Label
MarkMark() *Label
NoWrapNoWrap keeps the text on one line.NoWrap() *Label
SecondarySecondary uses the secondary text color.Secondary() *Label
SizeSize sets the font size (line height follows the theme).Size(px float32) *Label
StrongStrong uses the theme's strong weight.Strong() *Label
TertiaryTertiary uses the tertiary text color (descriptions, hints).Tertiary() *Label
TextDirTextDir sets the paragraph base direction explicitly.TextDir(d text.Direction) *Label
TypeType colors the text by status: "success", "warning", "danger"/"error".Type(status string) *Label
UnderlineUnderline / Delete / Code / Mark decorations.Underline() *Label
WeightWeight sets the font weight.Weight(w text.Weight) *Label

LinkW 方法 ​

ui.LinkW:LinkW is a clickable text link.

字段说明类型
Label*Label(嵌入)

LinkW 的方法:

方法说明签名
AlignAlign aligns the text in its box. (来自嵌入的 Label)Align(a TextAlign) *Label
ClickedClicked reports activation.Clicked() bool
Code(来自嵌入的 Label)Code() *Label
ColorColor draws the link in col in every state (disabled still greys out).Color(col theme.Color) *LinkW
CopiedCopied reports whether the copy icon was clicked within the last 3s. (来自嵌入的 Label)Copied() bool
CopyTextCopyText is Copyable with a different clipboard payload. (来自嵌入的 Label)CopyText(payload string) *Label
CopyableCopyable adds a copy icon after the text; clicking it writes the text to the clipboard and shows a check mark for three seconds. (来自嵌入的 Label)Copyable() *Label
Delete(来自嵌入的 Label)Delete() *Label
DisabledDisabled greys the text out. (来自嵌入的 Label)Disabled() *Label
EditableEditable adds an edit icon; clicking it swaps the text for an input. (来自嵌入的 Label)Editable(val *string) *Label
EditingEditing reports whether the label is being edited. (来自嵌入的 Label)Editing() bool
EllipsisEllipsis keeps one line and truncates with "…". (来自嵌入的 Label)Ellipsis() *Label
ExpandableExpandable adds an "展开" link when the text is cut by Lines(n); open is toggled by the link ("收起" collapses when open). (来自嵌入的 Label)Expandable(open *bool) *Label
HoveredHovered reports hover.Hovered() bool
IDID returns the node ID. (来自嵌入的 Label)ID() ID
IconIcon puts an icon before the text in the link color (Element el-link icon).Icon(ic *icon.Icon) *LinkW
ItalicItalic slants the text (synthetic oblique). (来自嵌入的 Label)Italic() *Label
LayoutLayout applies layout options to the text node. (来自嵌入的 Label)Layout(opts ...layout.Option) *Label
LineHeightLineHeight overrides the line height in px. (来自嵌入的 Label)LineHeight(px float32) *Label
LinesLines limits wrapping to n lines with an ellipsis. (来自嵌入的 Label)Lines(n int) *Label
Mark(来自嵌入的 Label)Mark() *Label
NoWrapNoWrap keeps the text on one line. (来自嵌入的 Label)NoWrap() *Label
SecondarySecondary is Type("secondary").Secondary() *LinkW
SizeSize sets the font size (line height follows the theme). (来自嵌入的 Label)Size(px float32) *Label
StrongStrong uses the theme's strong weight. (来自嵌入的 Label)Strong() *Label
TertiaryTertiary uses the tertiary text color (descriptions, hints). (来自嵌入的 Label)Tertiary() *Label
TextDirTextDir sets the paragraph base direction explicitly. (来自嵌入的 Label)TextDir(d text.Direction) *Label
TypeType colors the link by status: "success", "warning", "danger" / "error", "info" use that palette (hover and pressed shades follow); "secondary" (or Element's "default") starts in the secondary text color and turns link-colored on hover; "primary" is the primary palette.Type(status string) *LinkW
UnderlineUnderline / Delete / Code / Mark decorations. (来自嵌入的 Label)Underline() *Label
UnderlineModeUnderlineMode sets when the link is underlined: "always", "hover" or "never" (Element underline; the default follows the theme: never for Ant, on hover for Element, always for shadcn).UnderlineMode(mode string) *LinkW
WeightWeight sets the font weight. (来自嵌入的 Label)Weight(w text.Weight) *Label

相关链接 ​

以 MIT 许可发布