口红 0.5.2

口红 0.5.2

测试已测试
语言语言 SwiftSwift
许可 MIT
发布上次发布2017年3月
SwiftSwift 版本3.0
SPM支持 SPM

Tadeas Kriz 维护。



口红 0.5.2

口红

Slack Status

介绍

Lipstick 是一个小型库,旨在改进 UIKit (UIColor, CGRect ...) 的使用。它主要由便利初始化、辅助方法等组成。该库背后的主要动机是 Stylable 协议,它允许轻松地将样式应用于 UIView (请参阅使用说明/Stylable)。

更新日志

所有更改和新功能列表可以在此处找到 这里

要求

  • Swift 3
  • iOS 8+

使用方法

由于此库的特性,了解其内容的最佳方式是查看 测试 或直接查看 源代码。以下是完整内容的列表(按字母顺序排序,除了 Stylable),供快速参考。

注意:创建了所有类似 CGRect 的结构体的初始化,以便可以省略任何参数(默认值为 0)。例如:CGRect(x: 1)CGRect(origin: origin, width: 1)CGRect(x: 1, size: size) 都是有效的,并且这些可能性已从本说明中省略。

Styleable

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
    }
}

CGAffineTransform

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

说明:rotatetranslatescale 都是全局函数。它们创建相应的 CGAffineTransform 对象。所有这些都具有默认值(scale 的默认值为 1)。

CGPoint

extension CGPoint {

    init(_ both: CGFloat)

    init(x: CGFloat, y: CGFloat)
}

CGRect

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)
}

CGSize

extension CGSize {

    init(_ both: CGFloat)

    init(width: CGFloat, height: CGFloat)
}

NSAttributedString

+ 运算符

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

UIButton

extension UIButton {

    init(title: String)

    func setBackgroundColor(_ color: UIColor, forState state: UIControlState)
}

UICollectionView

extension UICollectionView {

    init(collectionViewLayout layout: UICollectionViewLayout)
}

UIColor

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
}

UIEdgeInsets

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)
}

UIFont

extension UIFont {

    init(_ name: String, _ size: CGFloat)
}

UILabel

extension UILabel {

    init(text: String)
}

UIOffset

extension UIOffset {

    init(_ all: CGFloat)

    init(horizontal: CGFloat)

    init(vertical: CGFloat)
}

UITableView

extension UITableView {

    init(style: UITableViewStyle)
}

版本管理

此库使用语义版本控制。在版本 1.0 之前,即使是小版本也可能发生 API 破坏性更改。我们将 0.5 版本视为预发布版本,这意味着 API 应该是稳定的,但尚未在实际项目中测试。在此测试之后,我们进行必要的调整,并将版本提升到 1.0(首次发布)。

作者

测试中使用的库

许可证

Lipstick 根据 MIT 许可证 提供。