---
title: "#143: Declarative Attributed Strings with BonMot 🖋"
url: https://littlebitesofcocoa.com/143-declarative-attributed-strings-with-bonmot
author: Jake Marsh
date: 2015-12-09
published: 2015-12-09
topics: ["Libraries","UIKit"]
description: "Attributed strings are a fantastic way to work with styled text on iOS and OS X. NSAttributedString is an incredibly full-featured API, but because of this..."
---

# #143: Declarative Attributed Strings with BonMot 🖋

By Jake Marsh, December 9, 2015. HTML: https://littlebitesofcocoa.com/143-declarative-attributed-strings-with-bonmot

_Attributed strings_ are a fantastic way to work with styled text on **iOS** and **OS X**. _NSAttributedString_ is an incredibly full-featured API, but because of this, simple tasks sometimes require a fair amount of boilerplate code. Today we'll look at _BonMot_, a library from [Raizlabs][1] that simplifies the process of composing _attributed strings_.

_BonMot_ takes the form of a set of chainable **functions** that we can use to compose _attributed strings_:

``` swift
let fancyQuote = "Traveling through hyperspace ain't" +
                 "like dusting crops, farm boy.\n" +
                 " — Han Solo"

quoteLabel.attributedText = BONChain()
  .fontNameAndSize("AmericanTypewriter", 17.0)
  .lineHeightMultiple(1.8)
  .string(fancyQuote)
  .attributedString
```

_BonMot_ will do all the heavy lifting of applying **attributes** and return a fully-formed _NSAttributedString_ ready for use wherever we need.





<img src='https://media.littlebitesofcocoa.com/bite-assets/143-declarative-attributed-strings-with-bonmot-quote.png' style='max-width: 224px;' />

We can use _BonMot_ to concatenate multiple composed attributed strings:

``` swift
let chain = BONChain()

chain.appendLink(BONChain().string("one fish"))
chain.appendLink(BONChain().string("two fish"), separator:commaSpace)
chain.appendLink(BONChain().string("red fish").textColor(UIColor.redColor()), separator:commaSpace)
chain.appendLink(BONChain().string("blue fish").textColor(UIColor.blueColor()), separator:commaSpace)
```

<img src='https://media.littlebitesofcocoa.com/bite-assets/143-declarative-attributed-strings-with-bonmot-fish.png' style='max-width: 285px;' />

_BonMot_ even supports the **NSTextAttachment** parts of the _NSAttributedString_ API. We can generate an “image next to a label” like this:

``` swift
chain.appendLink(BONChain().image(ackbar))
chain.appendLink(
  BONChain()
    .fontNameAndSize("Futura-MediumItalic", 17.0)
    .string("It's a trap!"), separator:" ")
```

<img src='https://media.littlebitesofcocoa.com/bite-assets/143-declarative-attributed-strings-with-bonmot-trap.png' style='max-width: 141px;' />

We've only scratched the surface here, _BonMot_ has a ton of features to offer. More info about _BonMot_ can be found at [git.io/bonmot][2]

[1]: https://github.com/Raizlabs
[2]: https://git.io/bonmot
