November 3rd, 2015
Rich-text formatting on iOS and OS X is performed with the
NSAttributedString class. There are two components: a string value, and a set of substring ranges, each with an associated dictionary of attributes, which are a string key paired with an arbitrary value - since this is a Foundation class, these values must be of object types. These ranges cannot overlap. In a safe manner, attributed strings could be expressed as:
API users do not need to interact with the backing representation at a low level, however - instead, attributes can be set across a range, or set for an entire string at initialization time. For example, given a mutable attributed string with a blue background color set across the entire string, adding a blue foreground color attribute to a middle range will automatically create three separate ranges: “”, “ ”, and “ ” again, each associated with the proper substring.