All Projects → kovpas → Bostring

kovpas / Bostring

Licence: mit
Create NSAttributedString like a boss!

Projects that are alternatives of or similar to Bostring

Swiftyattributes
A Swifty API for attributed strings
Stars: ✭ 1,303 (+575.13%)
Mutual labels:  cocoapods, nsattributedstring
Attributedstring
基于Swift插值方式优雅的构建富文本, 支持点击长按事件, 支持不同类型过滤, 支持自定义视图等.
Stars: ✭ 294 (+52.33%)
Mutual labels:  cocoapods, nsattributedstring
Nvdate
📅 Swift4 Date extension library
Stars: ✭ 176 (-8.81%)
Mutual labels:  cocoapods
Cognitive Face Ios
iOS SDK for the Microsoft Face API, part of Cognitive Services
Stars: ✭ 191 (-1.04%)
Mutual labels:  cocoapods
Fradioplayer
A simple radio player framework for iOS, macOS, tvOS.
Stars: ✭ 183 (-5.18%)
Mutual labels:  cocoapods
L10n Swift
Localization of the application with ability to change language "on the fly" and support for plural form in any language.
Stars: ✭ 177 (-8.29%)
Mutual labels:  cocoapods
Cocoapods Generate
A CocoaPods plugin that allows you to easily generate a workspace from a podspec.
Stars: ✭ 187 (-3.11%)
Mutual labels:  cocoapods
Countrypicker
A simple, customizable Country picker for picking country or dialing code. 🇮🇳 🇯🇵 🇰🇷 🇩🇪 🇨🇳 🇺🇸 🇫🇷 🇪🇸 🇮🇹 🇷🇺 🇬🇧
Stars: ✭ 174 (-9.84%)
Mutual labels:  cocoapods
Podtobuild
An easy way to integrate CocoaPods into Bazel
Stars: ✭ 193 (+0%)
Mutual labels:  cocoapods
Collor
A declarative-ui framework for UICollectionView with great and useful features.
Stars: ✭ 182 (-5.7%)
Mutual labels:  cocoapods
Pull To Refresh.rentals Ios
This project aims to provide a simple and customizable pull to refresh implementation. Made in Yalantis
Stars: ✭ 2,171 (+1024.87%)
Mutual labels:  cocoapods
Emalertcontroller
EMAlertController is a beautiful alternative to the stock iOS UIAlertController
Stars: ✭ 182 (-5.7%)
Mutual labels:  cocoapods
Modernavplayer
ModernAVPlayer is a persistence AVPlayer wrapper
Stars: ✭ 179 (-7.25%)
Mutual labels:  cocoapods
Displayswitcher
Custom transition between two collection view layouts
Stars: ✭ 2,253 (+1067.36%)
Mutual labels:  cocoapods
Icimulator
Simulate camera functions on iOS Simulator with images, videos, or your MacBook Camera.
Stars: ✭ 177 (-8.29%)
Mutual labels:  cocoapods
Whatsnewkit
Showcase your awesome new app features 📱
Stars: ✭ 2,329 (+1106.74%)
Mutual labels:  cocoapods
Toactionsheet
A custom-designed reimplementation of the UIActionSheet control for iOS
Stars: ✭ 175 (-9.33%)
Mutual labels:  cocoapods
Glinapppurchase
Tinder Style InApp Purchase Banner
Stars: ✭ 180 (-6.74%)
Mutual labels:  cocoapods
M3u8parser
A light weight M3U8 parser. Support X-Key & X-Session-Key.
Stars: ✭ 187 (-3.11%)
Mutual labels:  cocoapods
Cltypinglabel
iOS UILabel with character by character typing /typewriter animation
Stars: ✭ 192 (-0.52%)
Mutual labels:  cocoapods

NSAttributedString  Build Status

It's not a secret that NSAttributedString API is far from perfect. Based on NSDictionary, it looks ugly, counter-OOP and hard to maintain...

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;

Some developers get really desperate and write tools like this... Which again proves that NSAttributedString API is far from perfect.

Masonry

I love masonry. I love its clarity, its syntax. If you are using autolayout in your project and still write constraints using NSLayoutConstraint API, you should definitely take a look at masonry. Clear and very brief syntax makes code much more human readable, easy to maintain and modify.

BOString

So, based on masonry syntax, I decided to create a similar framework, which will take away some pain of creating NSAttributedString:

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.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]);
    });
}];

While making a string you can specify ranges for attributes either with a block-based syntax as in the example above:

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

or set range for a specific attribute (with is an optional semantic filler):

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

If you don't specify range, full range of string will be used.

Which attributes BOString supports? It supports a lot of them:

font;
paragraphStyle;
foregroundColor;
backgroundColor;
ligature;
kern;
strikethroughStyle;
underlineStyle;
strokeColor;
strokeWidth;
shadow;
textEffect;           // iOS only
attachment;
link;
baselineOffset;
underlineColor;
strikethroughColor;
obliqueness;
expansion;
writingDirection;
verticalGlyphForm;
superscript;          // OS X only
cursor;               // OS X only
toolTip;              // OS X only
characterShape;       // OS X only
glyphInfo;            // OS X only
markedClauseSegment;  // OS X only
textAlternatives;     // OS X only

"Wait, you forgot NSTheVeryBestAndUsefulAttribute!"

Indeed, there are many CoreText attributes that are not defined as methods. I.e. kCTLanguageAttributeName, kCTCharacterShapeAttributeName, kCTBaselineClassAttributeName, etc. In this case you may use attribute method:

make.attribute(kCTLanguageAttributeName, @"jp");

Even more than just an NSAttributedString maker!

A couple of substring attribute setters. Set attributes for a first substring found:

NSAttributedString *result = [@"This is a string" bos_makeString:^(BOStringMaker *make) {
    make.first.substring(@"is", ^{
        make.foregroundColor([UIColor greenColor]);
    });
}];

or highlight every substring:

NSAttributedString *result = [@"This is a string" bos_makeString:^(BOStringMaker *make) {
    make.each.substring(@"is", ^{
        make.foregroundColor([UIColor greenColor]);
    });
}];

You can also apply attributes using regular expressions:

NSAttributedString *result = [@"This is a string" bos_makeString:^(BOStringMaker *make) {
    make.each.regexpMatch(@"\\ws", NSRegularExpressionCaseInsensitive, ^{
        make.foregroundColor([UIColor greenColor]);
    });
}];

Or regular expressions with groups matching:

NSAttributedString *result = [@"This is a string" bos_makeString:^(BOStringMaker *make) {
    make.first.regexpGroup(@"[^h](i\\w)\\s(\\w*)", NSRegularExpressionCaseInsensitive, ^{
        make.foregroundColor([UIColor greenColor]);
    });
}];

Shorthand

In order to avoid conflicts with any other frameworks, bos_ prefix is used for category methods. However shorthand methods without this prefix could be used if you add #define BOS_SHORTHAND in your prefix.pch file before importing BOString.h.

Documentation

Documentation in HTML is available here or on cocoadocs.org.

Supported platforms

  • iOS 6.0 and later.
  • OS X Mavericks (10.9) and later.

Should work fine on iOS 4.3+ and OS X 10.5+ (except of several attributes), but I haven't had a chance to test it.

Installation

The easiest way is to use CocoaPods:

In your Podfile

pod 'BOString'

and import it in a file you want to make strings like a boss:

#import "Bostring.h"

Contribution

Feel free to submit pull requests into a separate branch. Please don't submit pull requests to master.

License

BOString is released under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].