Text 文字
文字展示原語:消費全域排版令牌,支援字級級別、字重、色調與對齊。
契約源:
lh-ui-core/specs/text.json· 黃金用例:lh-ui-core/cases/text.json(5 條,已翻譯為 hypium 測試)
字級級別
level 映射到 typography.fontSize.<level> 令牌,預設 base。
特大 extraLarge
大 large
中 medium
基準 base(預設)
小 small
特小 extraSmall
HTML 模擬預覽(鴻蒙 / ArkUI 外觀),非真機渲染
LhText({ content: '標題文字', level: LhTextLevel.Large })字重
weight 映射到 typography.fontWeight.<weight> 令牌(primary 400 / bold 600)。
常規 primary(預設)
加粗 bold
HTML 模擬預覽(鴻蒙 / ArkUI 外觀),非真機渲染
色調
tone 映射到 color.text.<tone> 令牌;selected 取 color.selected。
主要 primary(預設)
常規 regular
次要 secondary
佔位 placeholder
停用 disabled
選取 selected
HTML 模擬預覽(鴻蒙 / ArkUI 外觀),非真機渲染
對齊與行高
align 決定文字在行盒寬度內的水平定位(不是設計令牌,映射到框架列舉);行高恆取 typography.lineHeight.primary,layout 高度 = 字級 × 行高。
左對齊 start(預設)
置中 center
右對齊 end
HTML 模擬預覽(鴻蒙 / ArkUI 外觀),非真機渲染
LhText({ content: '右對齊', align: LhTextAlign.End })明暗主題
切換站台右上角主題,模擬器手機框內的文字顏色即變;真機透過 LhTheme.apply(true) 切換。
大標題
本文主色
次要說明
HTML 模擬預覽(鴻蒙 / ArkUI 外觀),非真機渲染;明/暗值全部取自 core 令牌快照
API
Props
| 屬性 | 說明 | 型別 | 預設值 |
|---|---|---|---|
content | 文字內容(spec 的 default 插槽) | string | '' |
level | 字級級別,映射到 typography.fontSize.<level> | LhTextLevel | Base |
weight | 字重,映射到 typography.fontWeight.<weight> | LhTextWeight | Primary |
tone | 文字色調,映射到 color.text.<tone> 或 color.selected | LhTextTone | Primary |
align | 水平對齊方式(在行盒寬度內生效) | LhTextAlign | Start |
LhTextLevel
| 值 | fontSize |
|---|---|
ExtraLarge | typography.fontSize.extraLarge(20) |
Large | typography.fontSize.large(18) |
Medium | typography.fontSize.medium(16) |
Base | typography.fontSize.base(14) |
Small | typography.fontSize.small(13) |
ExtraSmall | typography.fontSize.extraSmall(12) |
LhTextWeight
| 值 | fontWeight |
|---|---|
Primary | typography.fontWeight.primary(400) |
Bold | typography.fontWeight.bold(600) |
LhTextTone
| 值 | 文字色 |
|---|---|
Primary | color.text.primary |
Regular | color.text.regular |
Secondary | color.text.secondary |
Placeholder | color.text.placeholder |
Disabled | color.text.disabled |
Selected | color.selected |
LhTextAlign
| 值 | 端側映射 |
|---|---|
Start | TextAlign.Start |
Center | TextAlign.Center |
End | TextAlign.End |
令牌對照
文字消費的令牌(與 specs/text.json 的 tokenRefs 逐條對齊,全部是全域令牌—— text 沒有元件級令牌):
| 令牌 | 用途 |
|---|---|
typography.fontSize.{extraLarge,large,medium,base,small,extraSmall} | 六檔字級 |
typography.fontWeight.{primary,bold} | 兩檔字重 |
typography.lineHeight.primary | 行高(layout 高度 = 字級 × 行高) |
color.text.{primary,regular,secondary,placeholder,disabled} | 五檔文字色調 |
color.selected | 選取態文字色 |
黃金用例對照
cases/text.json 的 5 條斷言與 hypium 測試的對應關係(entry/src/test/Text.test.ets):
| case id | 斷言 | 本地測試 |
|---|---|---|
default-level-uses-base-tokens | 預設級消費 base 套排版令牌 | default_level_uses_base_tokens |
large-level-uses-large-token | large 字級令牌生效,base 未誤用 | large_level_uses_large_token |
bold-weight-uses-bold-token | bold 字重令牌生效,primary 未誤用 | bold_weight_uses_bold_token |
secondary-tone-uses-text-secondary | secondary 文字色令牌生效 | secondary_tone_uses_text_secondary |
render-emits-single-text-command | 指令流僅一條 TEXT 指令 | resolve_produces_single_complete_text_style |
另有暗色專項:dark_colors_come_from_dark_tokens / dimensions_are_identical_in_dark_mode。 執行:DevEco Studio 右鍵 entry/src/test → Run Tests。

