Lipstick 是一个小型库,旨在改进 UIKit (UIColor, CGRect ...) 的使用。它主要由便利初始化、辅助方法等组成。该库背后的主要动机是 Stylable
协议,它允许轻松地将样式应用于 UIView
(请参阅使用说明/Stylable)。
所有更改和新功能列表可以在此处找到 这里。
由于此库的特性,了解其内容的最佳方式是查看 测试 或直接查看 源代码。以下是完整内容的列表(按字母顺序排序,除了 Stylable),供快速参考。
注意:创建了所有类似 CGRect 的结构体的初始化,以便可以省略任何参数(默认值为 0)。例如:CGRect(x: 1)
、CGRect(origin: origin, width: 1)
和 CGRect(x: 1, size: size)
都是有效的,并且这些可能性已从本说明中省略。
Styleable
允许您轻松将定义视图外观的代码与其它代码分离。它基本上是使用闭包修改传入对象的语法糖。
protocol Styleable { }
extension UIView: Styleable { }
typealias Style<T> = (T) -> Void
extension Styleable {
func apply(style: Style<Self>)
func apply(styles: Style<Self>...)
func apply(styles: [Style<Self>])
func styled(using styles: Style<Self>...) -> Self
func styled(using styles: [Style<Self>]) -> Self
func with(_ style: Style<Self>) -> Self
}
我们建议将这些样式放入结构体 Styles
中,并将其嵌套到如下扩展中
class SomeView: UIView {
private let label = UILabel().styled(using: Styles.blueBackground)
}
fileprivate extension SomeView {
fileprivate struct Styles {
static func blueBackground(_ view: UILabel) {
view.backgroundColor = UIColor.blue
}
static func whiteBackground(_ view: UILabel) {
view.backgroundColor = UIColor.whiteBackground
}
}
}
要更改视图的外观,可以进行以下操作
class SomeView: UIView {
private let label ...
func changeAppearanceOfLabel() {
label.apply(style: Styles.whiteBackground)
}
}
可以使用具有闭包的静态变量而不是函数,如下所示
static var style: Style<UILabel> = { view in
view.backgroundColor = UIColor.blue
}
或任何让您满意的任何语法。
您还可以定义一些全局的基本样式,然后从其他样式中调用它们,如下所示
struct BaseStyles {
static func blueBackground(_ view: UIView) {
view.backgroundColor = UIColor.blue
}
}
struct LabelStyles {
static func yellowTintWithBlueBackground(_ label: UILabel) {
label.apply(style: BaseStyles.blueBackground)
label.tintColor = UIColor.yellow
}
}
func + (lhs: CGAffineTransform, rhs: CGAffineTransform) -> CGAffineTransform
func rotate(_ degrees: CGFloat) -> CGAffineTransform
func translate(x: CGFloat, y: CGFloat) -> CGAffineTransform
func scale(x: CGFloat, y: CGFloat) -> CGAffineTransform
说明:rotate
、translate
和 scale
都是全局函数。它们创建相应的 CGAffineTransform
对象。所有这些都具有默认值(scale
的默认值为 1)。
extension CGPoint {
init(_ both: CGFloat)
init(x: CGFloat, y: CGFloat)
}
extension CGRect {
init(x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat)
init(x: CGFloat, y: CGFloat, size: CGSize)
init(origin: CGPoint, width: CGFloat, height: CGFloat)
}
extension CGSize {
init(_ both: CGFloat)
init(width: CGFloat, height: CGFloat)
}
+
运算符func + (lhs: NSAttributedString, rhs: NSAttributedString) -> NSAttributedString
func + (lhs: String, rhs: NSAttributedString) -> NSAttributedString
func + (lhs: NSAttributedString, rhs: String) -> NSAttributedString
/// Enum which represents NS attributes for NSAttributedString (like NSStrokeColorAttributeName). Each case has value and assigned name.
enum Attribute {
...
...
...
var name: String
var value: AnyObject
}
extension Sequence where Iterator.Element == Attribute {
/// Creates dictionary from sequence of attributes by merging them together. String is name of case and AnyObject value corresponding to it.
func toDictionary() -> [String: AnyObject]
}
extension String {
func attributed(_ attributes: [Attribute]) -> NSAttributedString
func attributed(_ attributes: Attribute...) -> NSAttributedString
}
/// Returns input / 100.
postfix func %(input: CGFloat) -> CGFloat
extension UIButton {
init(title: String)
func setBackgroundColor(_ color: UIColor, forState state: UIControlState)
}
extension UICollectionView {
init(collectionViewLayout layout: UICollectionViewLayout)
}
extension UIColor {
/// Accepted formats: "#RRGGBB" and "#RRGGBBAA".
init(hex: String)
init(rgb: UInt)
init(rgba: UInt)
/// Increases color's brightness.
func lighter(by percent: CGFloat) -> UIColor
/// Reduces color's brightness.
func darker(by percent: CGFloat) -> UIColor
/// Increases color's saturation.
func saturated(by percent: CGFloat) -> UIColor
/// Reduces color's saturation.
func desaturated(by percent: CGFloat) -> UIColor
/// Increases color's alpha.
func fadedIn(by percent: CGFloat) -> UIColor
/// Reduces color's alpha.
func fadedOut(by percent: CGFloat) -> UIColor
}
extension UIEdgeInsets {
init(top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat)
init(_ all: CGFloat)
init(horizontal: CGFloat, vertical: CGFloat)
init(horizontal: CGFloat, top: CGFloat, bottom: CGFloat)
init(vertical: CGFloat, left: CGFloat, right: CGFloat)
}
extension UIFont {
init(_ name: String, _ size: CGFloat)
}
extension UILabel {
init(text: String)
}
extension UIOffset {
init(_ all: CGFloat)
init(horizontal: CGFloat)
init(vertical: CGFloat)
}
extension UITableView {
init(style: UITableViewStyle)
}
此库使用语义版本控制。在版本 1.0 之前,即使是小版本也可能发生 API 破坏性更改。我们将 0.5 版本视为预发布版本,这意味着 API 应该是稳定的,但尚未在实际项目中测试。在此测试之后,我们进行必要的调整,并将版本提升到 1.0(首次发布)。
Lipstick 根据 MIT 许可证 提供。