申请 0.6.2

申请 0.6.2

测试已测试
语言语言 SwiftSwift
许可 MIT
发布上次发布2023年3月
SPM支持 SPM

Morten Faarkrog 维护。



申请 0.6.2

Apply

Version License Platform

该项目是一个框架,用于简化创建、更改和维护 iOS 应用程序的 UI。

这是通过以下方式实现的:

  • 省略了设置代码视图时需要的许多样板代码,
  • 通过提供一个简单的 API 以更改属性并允许函数的链接,使 UI 代码更易于阅读,
  • 提高代码的复用性,并减少应用程序中的代码量,
  • 在单一位置定义 UI 风格,这样可以将样式组合起来,
  • 为视图维护类型安全,以避免需要强制类型转换。

该框架支持大多数 UIKit 组件及其属性。如果您发现任何缺失的部分,请创建一个 pull request 或打开一个 issue。感谢🎉

Interface Builder?

需要注意的是,这个框架不仅适用于那些完全使用代码创建视图的开发者,也适用于那些依赖于 Interface Builder (IB) 的开发者。如果您使用 IB 与此框架一起使用,我建议在代码中设置所有视图属性(字体、颜色等),只使用 IB 来设置布局和约束。这使得以后更改样式变得更加容易。

在示例项目中可以看到如何实现这一点。

要求:

  • iOS 8.0+
  • Xcode 8.0+
  • Swift 3.0+

安装

使用CocoaPods安装,只需在Podfile中添加以下行:

Swift 4.2及更高版本

pod "Apply"

Swift 4

pod "Apply", '0.5.0'

Swift 3.2

pod "Apply", '0.3.0'

示例

要运行示例项目,首先克隆仓库,然后从Example目录中运行pod install

用法

每个apply函数都会修改调用的视图的一些属性,并返回相同的视图。

由于你可能不需要总是使用返回的视图,调用函数的结果可以被丢弃而不会收到警告(@discardableResult)。这意味着你可以做以下操作

let button = UIButton(type: .system)
  .apply(title: “#suchchaining”)
  .apply(titleFont: UIFont.systemFont(ofSize: 18))

… 和 …

let button = UIButton(type: .system)
  .apply(title: “#suchchaining”)

// some time later…
button.apply(borderColor: .black, borderWidth: 1)

应用视图属性

如上所示,使用.apply非常简单。它支持大多数UIKit组件及其属性,并且随着新iOS版本中新特性的增加而更新。

使用示例

let titleLabel: UILabel = UILabel()
  .apply(font: UIFont.systemFont(ofSize: 24))
  .apply(textColor: .black)
  .apply(textAlignment: .center)
  .apply(text: "Title label")
  .apply(translatesAutoresizingMaskIntoConstraints: false)
  
let actionButton: UIButton = UIButton(type: .system)
  .apply(titleFont: UIFont.systemFont(ofSize: 18))
  .apply(titleColor: .red)
  .apply(title: "Action button")
  .apply(borderColor: .black, borderWidth: 1)
  .apply(translatesAutoresizingMaskIntoConstraints: false)

let containerView: UIView = UIView()
  .apply(shadowRadius: 2, shadowOpacity: 0.5, shadowColor: .black, shadowOffset: CGSize(width: 0, height: 2))
  .apply(backgroundColor: .lightGray)
  .apply(cornerRadius: 8)
  .apply(clipsToBounds: true)
  
let tableView = UITableView()
  .apply(isPagingEnabled: true)
  .apply(bounces: false)
  .apply(showsScrollIndicator: false)

lazy var stackView: UIStackView = {
  return UIStackView(arrangedSubviews: [self.titleLabel, self.actionButton])
    .apply(axis: .vertical)
    .apply(spacing: 10)
}()

样式

应用样式

在应用程序中,你经常会遇到需要重复使用相同样式的视图元素。使用Apply,设置和应用样式非常简单。在你的应用程序中某个地方,你可以定义可以应用于特定类型UI元素的视图样式。

在下面的示例中,有一个可重用的样式用于在应用程序中以标题形式显示的 UILabel,以及一个利用该样式的标签。

let titleLabelStyle = UIViewStyle<UILabel> {
  $0.apply(font: UIFont.systemFont(ofSize: 20))
    .apply(textColor: .red)
}

let titleLabel = UILabel()
  .apply(style: titleLabelStyle)
  .apply(text: "#verytitle”)

合并和应用样式

为了使阅读、创建、修改和维护样式更加容易,它们已经被设置为可以合并。

let borderedViewStyle = UIViewStyle<UIView> {
  $0.apply(borderColor: .black, borderWidth: 1)
}

let roundedViewStyle = UIViewStyle<UIView> {
  $0.apply(cornerRadius: 8)
}

let roundedBorderedViewStyle = borderedViewStyle.composing(with: roundedViewStyle)

let view = UIView()
  .apply(style: roundedBorderedViewStyle)
  .apply(backgroundColor: .red)

样式表

你在项目中如何使用和组织样式完全取决于你。可能有用的是引入一个类/结构体 Stylesheet 或类似的,在其中添加所有应用程序的颜色、字体、可重用样式以及其他你可能需要的内容。这样,更容易阅读和维护代码,并确保应用程序中UI的一致性。

此类样式表的示例可能如下所示

struct Stylesheet {

  struct Colors {
    static let primary = UIColor.red
    static let secondary = UIColor.blue
  }

  struct Fonts {
    static let title = UIFont.systemFont(ofSize: 24)
    static let h1 = UIFont.systemFont(ofSize: 20)
  }

  struct Styles {
    static let titleLabelStyle = UIViewStyle<UILabel> {
      $0.apply(font: Stylesheet.Fonts.title)
        .apply(textColor: Stylesheet.Colors.primary)
    }
  }

}

然后可以在应用程序的其他地方这样使用

let titleLabel = UILabel()
  .apply(style: Stylesheet.Styles.titleLabelStyle)
  .apply(text: "#muchtitle #suchwow")

还可以在示例项目中看到利用可切换主题的另一个示例。

通讯

  • 如果你发现了一个错误,请提交问题。
  • 如果你有功能请求,请提交问题。
  • 如果你想贡献,请提交一个拉取请求。代码目前遵循 RayWenderlich Swift样式指南

待办事项

  • ...

作者

莫滕·弗尔斯克罗格, @MFaarkrog

许可

本项目适用于MIT许可。更多信息请查看LICENSE文件。