Skip to content

Button 按钮 ​

基础操作按钮。触发即时操作,支持类型、尺寸、加载、禁用、图标与国风花纹底。

契约源:lh-ui-core/specs/button.json · 黄金用例:lh-ui-core/cases/button.json(8 条,已翻译为 XCTest,macOS 宿主即可运行)

类型 ​

六种类型决定底色与文字色。

9:41Button 类型

HTML 模拟预览(iOS / SwiftUI 外观),非真机渲染

swift
LhButton(onClick: {}, type: .primary, text: "主要按钮")

尺寸 ​

三档尺寸映射到 button.height / paddingHorizontal / fontSize / iconSize 令牌。 iOS 形态为胶囊圆角(radius.round 令牌)。

9:41Button 尺寸

HTML 模拟预览(iOS / SwiftUI 外观),非真机渲染

状态 ​

禁用与加载中都不触发 onClick;两者共存时按禁用优先处理(黄金用例 disabled-precedes-loading)。

9:41Button 状态

HTML 模拟预览(iOS / SwiftUI 外观),非真机渲染

图标与花纹底 ​

图标名按本地图标体系解析(LhButtonStyle.icon(for:) 映射 SF Symbol:search → magnifyingglass、 refresh → arrow.clockwise、check → checkmark、close → xmark,未登记名不绘制)。 花纹底消费 color.decoration 令牌,Canvas 绘制 45° 斜纹,叠在底色之下、文字之上但不影响对比度。

9:41图标与花纹

HTML 模拟预览(iOS / SwiftUI 外观),非真机渲染

明暗主题 ​

切换站点右上角主题,模拟器手机框内的按钮颜色即变;真机通过 LhTheme(isDark: true) 切换。

9:41暗色按钮

HTML 模拟预览(iOS / SwiftUI 外观),非真机渲染;明/暗值全部取自 core 令牌快照

API ​

Props ​

属性说明类型默认值
onClick点击回调;disabled 或 loading 为 true 时不触发() -> Void—
type按钮类型,决定底色与文字色LhButtonType.default
size按钮尺寸,映射到尺寸令牌LhButtonSize.medium
disabled是否禁用Boolfalse
loading是否加载中(显示指示器,不触发 onClick)Boolfalse
icon图标名(SF Symbol 映射解析)String?nil
bgPattern是否绘制国风花纹底纹Boolfalse
label / text按钮文字内容(spec 的 default 插槽)Text / 字符串—

LhButtonType ​

值底色文字色
.defaultcolor.background.defaultcolor.text.primary
.primarycolor.primary.defaultcolor.onPrimary
.successcolor.success.defaultcolor.onPrimary
.warningcolor.warning.defaultcolor.onPrimary
.dangercolor.danger.defaultcolor.onPrimary
.infocolor.info.defaultcolor.onPrimary

LhButtonSize ​

值heightpaddingHorizontalfontSizeiconSize
.smallbutton.height.small(32pt)button.paddingHorizontal.small(16pt)button.fontSize.small(13pt)button.iconSize.small(16pt)
.mediumbutton.height.medium(40pt)button.paddingHorizontal.medium(20pt)button.fontSize.medium(14pt)button.iconSize.medium(18pt)
.largebutton.height.large(48pt)button.paddingHorizontal.large(24pt)button.fontSize.large(16pt)button.iconSize.large(20pt)

禁用态覆盖:底色 color.disabled.bg、文字 color.disabled.text、描边 color.disabled.border。

令牌对照 ​

按钮消费的令牌(与 specs/button.json 的 tokenRefs 逐条对齐):

令牌用途
button.height.{small,medium,large}三档高度
button.paddingHorizontal.{small,medium,large}三档横向内边距
button.fontSize.{small,medium,large}三档字号
button.iconSize.{small,medium,large}三档图标尺寸
button.borderRadius / button.borderWidth圆角(跟随 radius.base)与描边宽度
button.fontWeight字重(跟随 typography.fontWeight.primary,端侧映射 Font.Weight)
color.primary.default / color.onPrimary主色类型底/文字
color.{success,warning,danger,info}.default语义类型底色
color.background.default / color.text.primary / color.border.base默认类型底/文字/描边
color.disabled.{bg,text,border}禁用态三色
color.decoration花纹底纹色
shadow.lighter默认 elevation(LhShadow 载体)
spacing.xs图标与文字间距

黄金用例对照 ​

cases/button.json 的 8 条断言与 XCTest 的对应关系:

case id断言本地测试
click-emits-onclickgate 放行(可交互)LhButtonTests.testEnabledButtonIsInteractive
disabled-blocks-click禁用态不可交互testDisabledIsNotInteractive
loading-blocks-click加载态不可交互testLoadingIsNotInteractive
disabled-precedes-loading禁用优先 + 禁用样式testDisabledPrecedesLoading
default-type-renders-text默认类型中性底/文字testDefaultTypeUsesNeutralTokens
primary-type-uses-primary-tokens主色类型消费主色令牌testPrimaryTypeUsesPrimaryTokens
small-size-uses-small-tokens小号消费 small 尺寸令牌testSmallSizeUsesSmallTokens
bgpattern-uses-decoration-token花纹底消费装饰色testBgPatternUsesDecorationToken

另有暗色专项:testDarkColorsComeFromDarkTokens / testDisabledInDarkUsesDarkDisabledTokens / testDimensionsAreIdenticalInDarkMode。

bash
swift test    # macOS 宿主即可跑,无需模拟器