Skip to content

Tag 標籤 ​

用於標記和選擇的緊湊標籤。比按鈕更小,支援語義類型、描邊、圓角膠囊與關閉按鈕。

契約源:lh-ui-core/specs/tag.json · 黃金用例:lh-ui-core/cases/tag.json(6 條,已翻譯為 XCTest,macOS 宿主即可執行)

類型 ​

六種類型決定底色與文字色。plain 為描邊淺底形態(語義淺底 + 語義描邊)。

9:41Tag 類型
預設主要成功
警告危險資訊
描邊主要描邊成功

HTML 模擬預覽(iOS / SwiftUI 外觀),非真機渲染

swift
LhTag(text: "主要", type: .primary)
LhTag(text: "描邊成功", type: .success, plain: true)

尺寸與圓角 ​

三檔尺寸對應到 tag.height / paddingHorizontal / fontSize 令牌;round 把圓角換成高度的一半(膠囊)。

9:41Tag 尺寸
小號中號大號
方角圓角

HTML 模擬預覽(iOS / SwiftUI 外觀),非真機渲染

可關閉 ​

closable 在右側顯示關閉按鈕,點選觸發 onClose(黃金用例 closable-tap-emits-onclose)。

9:41Tag 關閉
可關閉可關閉

HTML 模擬預覽(iOS / SwiftUI 外觀),非真機渲染

swift
LhTag(text: "可關閉", type: .primary, closable: true) {
  // onClose
}

API ​

Props ​

屬性說明類型預設值
text標籤文字String—
type標籤類型,決定底色與文字色LhTagType.default
lhSize標籤尺寸,對應到尺寸令牌LhTagSize.medium
plain是否為描邊淺底形態Boolfalse
round是否為膠囊圓角(半徑 = 高度 / 2)Boolfalse
closable是否顯示關閉按鈕Boolfalse
onClose關閉按鈕點選回呼(() -> Void)?nil

LhTagType ​

值底色文字色
.defaultcolor.background.default + color.border.base 描邊color.text.primary
.primarycolor.primary.defaultcolor.onPrimary
.successcolor.success.defaultcolor.onPrimary
.warningcolor.warning.defaultcolor.onPrimary
.dangercolor.danger.defaultcolor.onPrimary
.infocolor.info.defaultcolor.onPrimary

plain = true 時:底色取 color.<type>.bg、描邊與文字取 color.<type>.default(.default 類型忽略 plain,黃金用例 default-type-renders-text)。

LhTagSize ​

值heightpaddingHorizontalfontSize
.smalltag.height.small(20pt)tag.paddingHorizontal.small(8pt)tag.fontSize.small(12pt)
.mediumtag.height.medium(24pt)tag.paddingHorizontal.medium(12pt)tag.fontSize.medium(13pt)
.largetag.height.large(28pt)tag.paddingHorizontal.large(16pt)tag.fontSize.large(14pt)

令牌對照 ​

令牌用途
tag.height.{small,medium,large}三檔高度
tag.paddingHorizontal.{small,medium,large}三檔橫向內邊距
tag.fontSize.{small,medium,large}三檔字號
tag.borderRadius / tag.borderWidth預設圓角(跟隨 radius.small)與描邊寬度
tag.gap文字與關閉按鈕間距(跟隨 spacing.xs)
color.{primary,success,warning,danger,info}.{default,bg}語義類型底色與描邊淺底
color.onPrimary語義類型文字色
color.background.default / color.text.primary / color.border.base預設類型三色

黃金用例對照 ​

case id斷言本地測試
default-type-renders-text預設類型中性底/文字,忽略 plaintestDefaultTypeUsesNeutralTokens / testDefaultTypeIgnoresPlain
primary-type-uses-primary-tokens主色類型消費主色與 onPrimarytestPrimaryTypeUsesPrimaryTokens
plain-uses-bg-tokensplain 消費語義 bg 與語義描邊testPlainUsesBgTokens
small-size-uses-small-tokens小號消費 small 尺寸令牌testSmallSizeUsesSmallTokens
round-makes-radius-half-heightround 圓角 == 高度 / 2testRoundMakesRadiusHalfHeight
closable-tap-emits-onclose關閉按鈕觸發 onClosetestClosableTagInitAndRender(手勢斷言需模擬器)

另有暗色專項:testDarkColorsComeFromDarkTokens / testPlainInDarkUsesDarkBgTokens / testDimensionsAreIdenticalInDarkMode。

bash
swift test --filter LhTagTests    # macOS 宿主即可執行,無需模擬器