Open Source Swift Library That Wraps NSAttributedString Providing A Cleaner Syntax

I’ve mentioned a number of projects for working with NSAttributedString, most recently a library allowing you to shape an attributed string with a UIBezierPath.

Here’s an open source Swift library called Stylize that provides a wrapper for NSAttributedString.

Stylize provides a much easier to read and maintain syntax for creating attributed strings than NSAttributed string.

This example shows the formatting of an attributing string using NSAttributedString:

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

And the same formatting applied much more cleanly with Stylize:

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)

You can find Stylize on Github here.

A nice library for creating attributed strings with a cleaner and more readable syntax.

Original article: Open Source Swift Library That Wraps NSAttributedString Providing A Cleaner Syntax

©2015 iOS App Dev Libraries, Controls, Tutorials, Examples and Tools. All Rights Reserved.

Leave a Reply

Your email address will not be published. Required fields are marked *