Get Updates By E-mail 

Article Written By:
Johann Dowa

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.


Submit A Resource

Have you created a useful tutorial, library or tool for iOS development that you would like to get in front of our 300,000+ monthly page views from iOS developers?

You can submit the url here.

The resources we feel will appeal to our readers the most will be posted on the front page.