Get Updates By E-mail 

Article Written By:
Johann Dowa

Open Source NSAttributedString Wrapper Library With A Lean Readable Syntax


Earlier this year I mentioned a great library called Masonry for working with auto-layout that allows you to control your layouts with a nice readable syntax.

Another API with an ugly syntax is NSAttributedString. Here’s a library from Pavel Kovpas inspired by Masonry that wraps NSAttributedString allowing you to use it with a clean Masonry like syntax called BOString.

BOString supports all NSAttributedString attributes, and there’s also some handy NSAttributedString substring setters.

As the readme shows you can take difficult to maintain code like this:

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Test attributed string" attributes:@{NSForegroundColorAttributeName: [UIColor greenColor], NSFontAttributeName: [fnt fontWithSize:20]}];
[attributedString addAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]
                                  , NSFontAttributeName: [fnt2 fontWithSize:10]
                                  , NSLigatureAttributeName: @2
                                  , NSBaselineOffsetAttributeName: @1}
                          range:NSMakeRange(3, 5)];
[attributedString addAttributes:@{NSForegroundColorAttributeName: [UIColor blueColor]
                                  , NSFontAttributeName: [fnt2 fontWithSize:30]}
                          range:NSMakeRange(6, 9)];
[attributedString addAttributes:@{NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle)
                                  , NSStrikethroughColorAttributeName: [UIColor redColor]
                                  , NSBackgroundColorAttributeName: [UIColor yellowColor]}
                          range:NSMakeRange(7, 4)];
_attributedTextView2.attributedText = attributedString;

And turn it into this:

NSMutableAttributedString *attributedString = [@"Test attributed string" bos_makeString:^(BOStringMaker *make) {
    make.foregroundColor([UIColor greenColor]);
    make.font([fnt fontWithSize:20]);

    make.with.range(NSMakeRange(3, 5), ^{
        make.foregroundColor([UIColor redColor]);
        make.font([fnt2 fontWithSize:10]);

    make.with.range(NSMakeRange(6, 9), ^{
        make.foregroundColor([UIColor blueColor]);
        make.font([fnt2 fontWithSize:30]);

    make.with.range(NSMakeRange(7, 4), ^{
        make.strikethroughColor([UIColor redColor]);
        make.backgroundColor([UIColor yellowColor]);

You can find BOString on Github here.

A very nice NSAttributedString wrapper library. Thanks to Pavel for the submission.


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.