Skip to content

trafi/TextStyle

Repository files navigation

Swift Package Manager compatible Carthage compatible

TextStyle

Helps you manage text styles in your app.

Describing text styles

Extend TextStyle with static variables for different text styles used in your app:

extension TextStyle {
    static let header = TextStyle()
        .with(font: .boldSystemFontOfSize(18))
        .with(color: .redColor())
        .with(alignment: .Center)
    
    static let body = TextStyle()
        .with(font: .systemFontOfSize(16))
    
    static let footer = TextStyle()
        .with(font: .italicSystemFontOfSize(12))
        .with(color: .grayColor())
}

Usage

From code

There's a set(textStyle:) defined for text-based UI elements. Just call it providing one of your styles.

let titleLabel = UILabel()
titleLabel.set(textStyle: .header)

From Interface Builder

Extend TextStyle conforming to IBInspectable protocol:

extension TextStyle: IBInspectable {
    static var stylesDictionary: [String : TextStyle] {
        return [
            "header": .header,
            "body":   .body,
            "footer": .footer
        ]
    }
}

In the Interface Builder set Text Style to match your style's name.

To see style changes right in the Interface Builder use Designable UI elements classes. (i.e. UILabelDesignable). You should and add them to your project manually.

Installation

Open your project in Xcode and select File > Swift Packages > Add Package Dependency. There enter https://github.com/trafi/TextStyle as the repository URL.

Add the following line to your Cartfile:

github "Trafi/TextStyle"