Button Kit by 44 是一个灵活且易于使用的工具箱,专为设计师和 iOS 开发者设计。作为设计师,您将获得一个 Sketch 文档,其中包含可直接使用且代码支持的现代按钮设计符号。作为开发者,您将获得一个 Swift Package,使您无需编写繁琐的样板代码或重复造轮子即可实现设计。
Sketch 文档中的符号旨在复制到您的项目中,并在那里手动编辑。Swift 代码旨在按原样使用,无需更改。您可以将其剥离并扩展为自己的代码,但如果这样做,将不再支持未来的更新。
从以下链接下载 Sketch 文件:https://github.com/tobberacing/Files/raw/master/ButtonKit.sketch
开始将符号添加到您的 Sketch 文档中
通过 Xcode 从以下链接添加 Swift Package 依赖项:https://github.com/tobberacing/ButtonKit.git
通过 IB 或以编程方式使用 Button 类
👉 https://www.tobiasrenstrom.com/portfolio/button-kit
该工具包以 Swift Package 的形式提供,其中包含一个类,支持 Sketch 设计中提供的所有变体。如果您和您的设计师想要扩展代码,可以简单地复制/粘贴整个 Button
类并将其变成您自己的。如果是这样,请注意将不再支持未来的更新。您也可以编写自己的扩展或子类,但请注意,这些可能会被破坏 - 即使更新尝试缓解这种情况。此工具包的精神在于按原样使用代码。
通过在 Interface Builder 中放置一个视图并将其类更改为 Button
,或者通过以下编程方式,将按钮实例化到您的项目中 👇
let defaultButton = Button(width: 200, color: nil, style: nil, size: nil)
或者推荐的、更快捷的便捷初始化方法,它还接受文本和 selectionBlock👇
let defaultButton = Button(width: 200, text: "Done", color: nil, style: nil, size: nil) {
// code
}
无论您在何处以编程方式使用 Button
类,都需要导入 ButtonKit
模块。
import ButtonKit
可以直接在 Interface Builder 中或以编程方式编辑按钮的外观。您可以调整的内容包括 👇
primary
👉 在 IB 中无需任何操作,以编程方式实例化时,样式参数使用 nil
。
secondary
👉 在 IB 中将 isBordered
* 属性设置为 true,或以编程方式实例化时使用 border
样式。
plain
👉 在 IB 中将 isPlain
* 属性设置为 true,或以编程方式实例化时使用 plain
样式。
Destructive
👉 在 IB 或以编程方式将 isDestructive
* 属性设置为 true。Destructive 不是像其他样式那样的样式,因为破坏性按钮可以是 primary
、secondary
或 plain
样式。
*由于 @IBInspectable 不支持枚举或任何类型的数组,因此 Interface Builder 中的样式设置不幸地需要是布尔值。
regular
👉 默认布局。
detailRight
👉 在 IB 或以编程方式设置 detailText
属性。
detailCenter
👉 在 IB 或以编程方式设置 detailText
属性和 isCenterred
属性为 true。
icon
👉 使用 UIImage
对象指定图标图像。这将覆盖任何 detailText
。在 IB 或以编程方式设置。
circle
👉 使用接受 diameter
的便捷初始化方法。
spinner
👉 调用 startSpinner()
进入 spinner 模式。stopSpinner()
退出。
有两种尺寸,regular
和 small
。要创建小尺寸按钮,可以以编程方式实例化时使用 Size
值,或通过 Interface Builder 使用 isSmallSize
属性。
默认高度为 50
和 40
磅。如果您的设计师想要更改这些值,您可以在应用加载期间进行更改,例如在您的 App Delegate 中,使用 regularHeight
和 smallHeight
设置。项目中的所有按钮都将调整其框架高度。
为了帮助在您的应用中保持一致的用户界面,这不能按按钮设置。例外情况是圆形按钮,它可以具有您定义的任何 diameter
。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Button.regularHeight = 44
Button.smallHeight = 30
return true
}
可以为按钮(具有纯色背景颜色)添加阴影,并使用 color
、opacity
、offset
和 radius
的自定义值。与您的设计师合作找到合适的值。由于 Sketch 和 iOS 渲染阴影的方式略有不同,因此需要进行一些调整。在此处阅读有关这些差异的更多信息:https://medium.com/@nathangitter/why-your-app-looks-better-in-sketch-3a01b22c43d7
defaultButton.shadowColor = UIColor.darkGray
defaultButton.shadowOpacity = 0.2
defaultButton.shadowOffset = CGSize(width: 0, height: 6)
defaultButton.shadowRadius = 9
更新文本、详细文本和图标图像的推荐方法是直接更改 Button
对象上的值。避免访问底层的 UILabel
或 UIImageView
对象。每当 Button
上存储的属性(text
、textColor
、detailText
...)发生更改时,都会触发样式更新以反映该更新。但是,由于此样式更新使用按钮本身上存储的值,因此直接在底层 UILabel
或 UIImageView
上设置的任何值都将被还原并丢失。
以下是一些可以直接在 Button
对象上更改的属性。查看代码以了解自本文档编写以来是否添加了更多属性。
public var color: UIColor?
public var text: String
public var detailText: String?
public var iconImage: UIImage?
public var textColor: UIColor?
public var borderColor: UIColor?
public var borderWidth: CGFloat
public var shadowOpacity: Float
public var shadowColor: UIColor
public var shadowOffset: CGSize
public var shadowRadius: CGFloat
public var cornerRadius: CGFloat
public var edgePadding: CGFloat
public var isFeedbackEnabled: Bool
可以通过使用 selectionBlock
、使用 target
/selector
模式或设置 delegate
来实现操作。Block 是推荐的最简单方法。
defaultButton.selectionBlock = { button in
// code
}
通过使用 addTarget
函数来使用 target
/selector
模式。
defaultButton.addTarget(self, selector: #selector(didSelect))
将 selector
指向要调用的 @objc
函数。
@objc func didSelect() {
// code
}
要改为为回调添加委托,请在按钮上设置 selectionDelegate
属性,并实现 ButtonSelectionDelegate
协议。selectionDelegate
也可以通过 Interface Builder 设置。
defaultButton.selectionDelegate = self
extension ViewController: ButtonSelectionDelegate {
func didTapButton(_ button: Button) {
// code
}
}
为了帮助在您的应用中保持一致的用户界面,许多设置可以通过静态属性定义为全局默认值。这些会影响所有按钮,除非为特定按钮设置了其他值。设置包括字体、破坏性颜色、高度和圆角半径。根据您的需要调整这些值。
static public var defaultColor = UIColor(hex: "293440") // overridable in instances by setting specific color
static public var defaultTextColor: UIColor? // overridable in instances by setting specific textColor
static public var destructiveColor = UIColor(hex: "FB0002")
static public var destructiveTextColor = UIColor(hex: "FFFFFF")
static public let regularFont = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.bold)
static public let smallFont = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.bold)
static public let regularDetailFont = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.bold)
static public let smallDetailFont = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.bold)
static public let regularCenterredDetailFont = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.bold)
static public let smallCenterredDetailFont = UIFont.systemFont(ofSize: 10, weight: UIFont.Weight.bold)
static public let iconSize = CGSize(width: 30, height: 30)
static public var regularCornerRadius: CGFloat?
static public var smallCornerRadius: CGFloat?
在应用加载期间,显示任何视图之前,在某个位置设置这些值。例如,在您的 App Delegate 或类似位置。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Button.defaultColor = UIColor.black
Button.defaultTextColor = UIColor.white
Button.smallCornerRadius = 1
Button.regularCornerRadius = 5
Button.destructiveColor = UIColor.red
Button.destructiveTextColor = UIColor.white
return true
}
代码以 Swift Package 的形式提供。在 Xcode 中,选择 File -> Swift Packages -> Add Packade Depency…
Package Repository 位于 👇
https://github.com/tobberacing/ButtonKit.git
这将下载源代码并将其作为 ButtonKit
模块添加到您的项目中。