NSAttributedString in Swift
How to deal with attributed string and live happy by theming your app using SwiftRichString 2
The following article is also available on my blog.
First release of SwiftRichString was take place in December 2016; as many other works it’s born for my own needs;
NSAttributedString management is one of the part of UIKit which owes lots from the Objective-C heritage and even if something is changed with Swift 4 it still have plenty room of improvement to fit well in a post Swift world.
SwiftRichString introduced the concept of
Style to define and collect a type safe collection of attributes you can apply to a plain or already-attributed string. The new 2.x branch is a total rewrite of the library which suits better with protocol oriented programming approach with several room for future improvements.
Main highlights includes:
- a new optimized codebase; with a protocol oriented approach we have more room to improve the library keeping it clean and typesafe.
- a new central repository for styles allows you to register styles with a name and used them all around the app without code duplication.
- integration with Interface Builder allows you set the style to UI controls (currently
UILabel, `UITextField`, `UITextView`) from globally registered styles and leave the library to render the content automatically!
Introducing Style , StyleGroup and StyleRegEx
My first goal was to create a generic protocol (called
StyleProtocol) with several methods which allows to apply the style’s attributes to plain/attributed string. From this protocol I’ve created three different concrete classes:
Style: this is the class which represent a collection of attributes you can apply to a string. Style is created anonymously (you don’t need to assign a name like in 1.x) and allows configuration via builder pattern approach. You can create as many styles you want, concatenate or derivate them. Since 1.x branch I’ve exposed lots of other CoreText properties (see documentation for more infos). Once created you can apply a style to a string by using string’s set or add method (the first replace any existing style, the last just sum attributes replacing only duplicates).
StyleGroup: old version of SwiftRichString exposes a custom functions to render tag-based content; a
StyleGroupis a container for named
Styleinstances (and optional base style). Once applied to a string it just parse the content by assigning attributes to tags content.
StyleRegEx: as for tag-based rendering even regex was a function in old version;
StyleRegExallows you to create a style which will be applied automatically to all matches of specified regular expression pattern.
All of these elements can be also mixed to produce a single attributed string because — in fact — each instance just produce a dictionary of
NSAttributedStringKey keys and value.
Use StyleManager to theme your app
Since 2.x SwiftRichString introduces the concept of global styles register; its a singleton class where you can register styles (simple style, group or regex style) with an unique identifier.
Once registered style can be reused to render text from inside UI controls or via code.
Suppose we want to create a
MyStylee style which allows to render a text with a common style and two variants identified by
The following code create three style and register its group to the global registry.
In order to keep our code clean we can define a non-instantiable struct to contains all registered style names:
Since now we can reuse it via code everywhere inside your app:
Super easy styling with Interface Builder
You can also set the style per UI control. In this example we assign
MyStyle as the style for an
UILabel ; once the label has a style assigned the content is rendered automatically with that style and you can update it by keeping the rendering by using
styledText instead of
Regular Expression Rendering via StyleRegEx
Tag based rendering is useful but you can also apply regular expression matching style to a string. This is done by creating an instance of the
That’s the result:
String & Attributed String Concatenation
Concatenation between plain String and Attributed String is super-easy with SwiftRichString as using the
Want to know more about all the opportunities of SwiftRichString? On GitHub project’s page you can found the complete documentation of all features of the project. SwiftRichString is available for all Apple’s platforms and can be installed via CocoaPods, Swift PM or Carthage.