Atelier Clockwork

BonMot

Attributed Strings Made Significantly Less Byzantine

figuring out how to build any non-trivial attributed string in iOS feels like something of a dark art. It involves building dictionaries of keys, setting attribute values for ranges, and and generally requiring large amounts of domain knowledge to do something that feels like it should be simple.

To address this, my co-worker Zev wrote BonMot. I hit a point where the project I was working on required several of the advanced tricks from BonMot, and pulling it into the project helped streamline the text handling flow throughout the app.

One large benefit is that the central style class included fontOfSize(size: CGFloat) -> BonChain class functions for the fonts used throughout the app, along with standard sizes for elements like headers, buttons, body text, and so forth. The chains could then have the appropriate extra attributes added, most often colors, set the text, and then generate the attributed string. This helped reduce style bloat significantly.

That’s just scratching the surface of the library, it also adds support for advanced constraints based on font metrics, image attachments, tracking, smart concatenation, and more.