Skip to content

Text 文字 ​

文字展示原語:消費全域排版令牌,支援字級級別、字重、色調與對齊。

契約源:lh-ui-core/specs/text.json · 黃金用例:lh-ui-core/cases/text.json(5 條,已翻譯為 hypium 測試)

字級級別 ​

level 映射到 typography.fontSize.<level> 令牌,預設 base。

9:41Text 字級

特大 extraLarge

大 large

中 medium

基準 base(預設)

小 small

特小 extraSmall

HTML 模擬預覽(鴻蒙 / ArkUI 外觀),非真機渲染

typescript
LhText({ content: '標題文字', level: LhTextLevel.Large })

字重 ​

weight 映射到 typography.fontWeight.<weight> 令牌(primary 400 / bold 600)。

9:41Text 字重

常規 primary(預設)

加粗 bold

HTML 模擬預覽(鴻蒙 / ArkUI 外觀),非真機渲染

色調 ​

tone 映射到 color.text.<tone> 令牌;selected 取 color.selected。

9:41Text 色調

主要 primary(預設)

常規 regular

次要 secondary

佔位 placeholder

停用 disabled

選取 selected

HTML 模擬預覽(鴻蒙 / ArkUI 外觀),非真機渲染

對齊與行高 ​

align 決定文字在行盒寬度內的水平定位(不是設計令牌,映射到框架列舉);行高恆取 typography.lineHeight.primary,layout 高度 = 字級 × 行高。

9:41Text 對齊

左對齊 start(預設)

置中 center

右對齊 end

HTML 模擬預覽(鴻蒙 / ArkUI 外觀),非真機渲染

typescript
LhText({ content: '右對齊', align: LhTextAlign.End })

明暗主題 ​

切換站台右上角主題,模擬器手機框內的文字顏色即變;真機透過 LhTheme.apply(true) 切換。

9:41暗色文字

大標題

本文主色

次要說明

HTML 模擬預覽(鴻蒙 / ArkUI 外觀),非真機渲染;明/暗值全部取自 core 令牌快照

API ​

Props ​

屬性說明型別預設值
content文字內容(spec 的 default 插槽)string''
level字級級別,映射到 typography.fontSize.<level>LhTextLevelBase
weight字重,映射到 typography.fontWeight.<weight>LhTextWeightPrimary
tone文字色調,映射到 color.text.<tone> 或 color.selectedLhTextTonePrimary
align水平對齊方式(在行盒寬度內生效)LhTextAlignStart

LhTextLevel ​

值fontSize
ExtraLargetypography.fontSize.extraLarge(20)
Largetypography.fontSize.large(18)
Mediumtypography.fontSize.medium(16)
Basetypography.fontSize.base(14)
Smalltypography.fontSize.small(13)
ExtraSmalltypography.fontSize.extraSmall(12)

LhTextWeight ​

值fontWeight
Primarytypography.fontWeight.primary(400)
Boldtypography.fontWeight.bold(600)

LhTextTone ​

值文字色
Primarycolor.text.primary
Regularcolor.text.regular
Secondarycolor.text.secondary
Placeholdercolor.text.placeholder
Disabledcolor.text.disabled
Selectedcolor.selected

LhTextAlign ​

值端側映射
StartTextAlign.Start
CenterTextAlign.Center
EndTextAlign.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-tokenlarge 字級令牌生效,base 未誤用large_level_uses_large_token
bold-weight-uses-bold-tokenbold 字重令牌生效,primary 未誤用bold_weight_uses_bold_token
secondary-tone-uses-text-secondarysecondary 文字色令牌生效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。