Giter Site home page Giter Site logo

stylize's Introduction

stylize

Build Status

A funcitonal wrapper of NSAttributedString for easy string styling

Contents

Why does NSAttributedString need a wrapper?

Styling strings with NSAttributedString requires a lot of painful and ugly boiler plate code, for example changing the color of a substring and underlining it requires:

let string = NSMutableAttributedString(string: "Hello")
string.addAttribute(NSforegroundAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, 5))
string.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleSingle.rawValue, range: NSMakeRange(0, string.length))

Ouch!

This quickly builds into a giant chunk of code that is a pain to read and maintain. Using stylize our code looks like this:

let string          = NSAttributedString(string: "Hello World")
let foregroundStyle = Stylize.foreground(UIColor.redColor(), range: NSMakeRange(0, 5))
let underlineStyle  = Stylize.underline(NSUnderlineStyle.StyleSingle)
let style           = Stylize.compose(foregroundStyle, underlineStyle)

let styledString    = style(string)

That's better.

Integration

Carthage

Add github "alexfish/stylize" to your Cartfile

Manual

iOS 8+

  1. Add Stylize to you project as a submodule using git submodule add https://github.com/alexfish/stylize.git
  2. Open the Stylize folder & drag Stylize.xcodeproj into your project tree
  3. Add Stylize.framework to your target's Link Binary with Libraries Build Phase
  4. Import Stylize with import Stylize and you're ready to go

iOS 7+

To manually install Stylize, download this repository and drag Stylize.swift into your project tree.

Usage

let string        = NSAttributedString(string: "Hello World")
let style         = Stylize.foreground(UIColor.redColor())
let styledString  = style(string)

Substrings

By default styles will be applied to the entire string, if you need to apply a style to a subsstring an optional range paramater is available for each style:

let string        = NSAttributedString(string: "Hello World")
let style         = Stylize.foreground(UIColor.redColor(), range: NSMakeRange(0, 5))
let styledString  = style(string)

Composing Styles

stylize has a compose function that can compose a style from multiple styles

let string          = NSAttributedString(string: "Hello World")
let foregroundStyle = Stylize.foreground(UIColor.redColor())
let backgroundStyle = Stylize.background(UIColor.orangeColor())
let underlineStyle  = Stylize.underline(NSUnderlineStyle.StyleSingle)

let style           = Stylize.compose(foregroundStyle, backgroundStyle, underlineStyle)
let styledString    = style(string)

Available Attributes

Attribute Function
NSUnderlineStyleAttributeName underline(style: NSUnderlineStyle)
NSUnderlineColorAttributeName underline(color: UIColor)
NSForegroundColorAttributeName foreground(color: UIColor)
NSBackgroundColorAttributeName background(color: UIColor
NSStrikethroughStyleAttributeName strikethrough(style: NSUnderlineStyle)
NSStrikethroughColorAttributeName strikethrough(color: UIColor)
NSLinkAttributeName link(url: NSURL)
NSParagraphStyleAttributeName paragraph(style: NSParagraphStyle)
NSKernAttributeName kern(points: NSNumber)
NSBaselineOffsetAttributeName baseline(offset: NSNumber)
NSShadowAttributeName shadow(shadow: NSShadow)
NSStrokeWidthAttributeName stroke(width: NSNumber)
NSStrokeColorAttributeName stroke(color: UIColor)
NSTextEffectAttributeName letterpress()
NSFontAttributeName font(font: UIFont)
NSLigatureAttributeName ligatures(enabled: Bool)
NSObliquenessAttributeName obliqueness(skew: NSNumber)
NSAttachmentAttributeName attachment(attachement: NSTextAttachment)
NSExpansionAttributeName expand(log: NSNumber)
NSWritingDirectionAttributeName direction(direction: WritingDirection)

stylize's People

Contributors

alexfish avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.