Lipstick 0.5.2

Lipstick 0.5.2

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Mar 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Tadeas Kriz.



Lipstick 0.5.2

Lipstick

Slack Status

Introduction

Lipstick is small library which aim to improve usage of UIKit (UIColor, CGRect …). It consists mostly of convenience inits, helper methods etc. Main motivation behind this library is the protocol Stylable which allows easy style application to UIView (See Usage/Stylable).

Changelog

List of all changes and new features can be found here.

Requirements

  • Swift 3
  • iOS 8+

Usage

Due to the nature of this library, the easiest way to learn its content is to look at tests or directly into source code. Below is listed complete content (sorted alphabetically except for Stylable) for quick reference.

Note: Inits for all structs like CGRect are created so that any parameter can be omitted (has default value of 0). For example: CGRect(x: 1), CGRect(origin: origin, width: 1) and CGRect(x: 1, size: size) are all valid and these possibilities are skipped from this documentation.

Styleable

Styleable allows you to easily separate code defining view appearance from the rest. It is basically syntax sugar for using closures which modify passed object.

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
}

We recommend to put these styles into struct Styles and nest it to extension like this:

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

To later change the appearance of view do:

class SomeView: UIView {

    private let label ...

    func changeAppearanceOfLabel() {
        label.apply(style: Styles.whiteBackground)
    }
}

It is possible to use static var with closure instead of function like this:

static var style: Style<UILabel> = { view in
    view.backgroundColor = UIColor.blue
}

Or any other syntax that you are happy with.

You can also define some base styles globally and then call them from another styles like so:

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

Notes: rotate, translate and scale are all global functions. They create corresponding CGAffineTransform. All of them have default values (scale has as default values 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

+ operator

func + (lhs: NSAttributedString, rhs: NSAttributedString) -> NSAttributedString

func + (lhs: String, rhs: NSAttributedString) -> NSAttributedString

func + (lhs: NSAttributedString, rhs: String) -> NSAttributedString

Attribute

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

String

extension String {

    func attributed(_ attributes: [Attribute]) -> NSAttributedString

    func attributed(_ attributes: Attribute...) -> NSAttributedString
}

Percent

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

Versioning

This library uses semantic versioning. Until version 1.0 API breaking changes may occur even in minor versions. We consider version 0.5 to be prerelease, which means that API should be stable but is not tested yet in real project. After that testing we make needed adjustments and bump the version to 1.0 (first release).

Author

Used libraries in tests

License

Lipstick is available under the MIT License.