ButtonKit,适用于 Sketch + Swift。

Button Kit by 44 是一个灵活且易于使用的工具箱,专为设计师和 iOS 开发者设计。作为设计师,您将获得一个 Sketch 文档,其中包含可直接使用且代码支持的现代按钮设计符号。作为开发者,您将获得一个 Swift Package,使您无需编写繁琐的样板代码或重复造轮子即可实现设计。

Sketch 文档中的符号旨在复制到您的项目中,并在那里手动编辑。Swift 代码旨在按原样使用,无需更改。您可以将其剥离并扩展为自己的代码,但如果这样做,将不再支持未来的更新。

快速入门指南

设计师请看 👇

  1. 从以下链接下载 Sketch 文件:https://github.com/tobberacing/Files/raw/master/ButtonKit.sketch

  2. 开始将符号添加到您的 Sketch 文档中

开发者请看 👇

  1. 通过 Xcode 从以下链接添加 Swift Package 依赖项:https://github.com/tobberacing/ButtonKit.git

  2. 通过 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 中或以编程方式编辑按钮的外观。您可以调整的内容包括 👇

样式

*由于 @IBInspectable 不支持枚举或任何类型的数组,因此 Interface Builder 中的样式设置不幸地需要是布尔值。

布局

尺寸

有两种尺寸,regularsmall。要创建小尺寸按钮,可以以编程方式实例化时使用 Size 值,或通过 Interface Builder 使用 isSmallSize 属性。

默认高度为 5040 磅。如果您的设计师想要更改这些值,您可以在应用加载期间进行更改,例如在您的 App Delegate 中,使用 regularHeightsmallHeight 设置。项目中的所有按钮都将调整其框架高度。

为了帮助在您的应用中保持一致的用户界面,这不能按按钮设置。例外情况是圆形按钮,它可以具有您定义的任何 diameter

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    Button.regularHeight = 44
    Button.smallHeight = 30
    
    return true
}

阴影

可以为按钮(具有纯色背景颜色)添加阴影,并使用 coloropacityoffsetradius 的自定义值。与您的设计师合作找到合适的值。由于 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 对象上的值。避免访问底层的 UILabelUIImageView 对象。每当 Button 上存储的属性(texttextColordetailText...)发生更改时,都会触发样式更新以反映该更新。但是,由于此样式更新使用按钮本身上存储的值,因此直接在底层 UILabelUIImageView 上设置的任何值都将被还原并丢失。

以下是一些可以直接在 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 模块添加到您的项目中。