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:
[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:
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.ligature(@2);
make.baselineOffset(@1);
});
make.with.range(NSMakeRange(6, 9), ^{
make.foregroundColor([UIColor blueColor]);
make.font([fnt2 fontWithSize:30]);
});
make.with.range(NSMakeRange(7, 4), ^{
make.strikethroughStyle(@(NSUnderlineStyleSingle));
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.
- Open Source: New XML Parsing Library With A Highly Readable Easy To Maintain Syntax
- Open Source: NSAttributedString HTML Additions For iOS
- Library Providing Easily Customizable HTML-Style Syntax For Rich Text With NSAttributedString
- Open Source iOS Library Providing A Clean High-Level Syntax For Integrating App.net
- Best Resources In iOS Development – October 3rd 2011
Original article: Open Source NSAttributedString Wrapper Library With A Lean Readable Syntax
©2014 iOS App Dev Libraries, Controls, Tutorials, Examples and Tools. All Rights Reserved.