CREasyStyleTextStorage 0.0.1

CREasyStyleTextStorage 0.0.1

LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Dec 2014

Maintained by Unclaimed.

  • By
  • Collin Ruffenach

CREasyStyleTextStorage is a NSTextStorage subclass that provides a convinent way to apply distinct styles to substrings. There is a demo project showing a general use case for it. PR's and Issues welcome. Lots more functionality to come.

Basic Usage

CREasyStyleTextStorage allows for specific strings to have unique styling. Configuring and using in a UITextView would look something like this.

CREasyStyleTextStorage *textStorage = [[CREasyStyleTextStorage alloc] init];

// Set default attributes

[textStorage setDefaultAttributes:@{NSFontAttributeName             : [UIFont systemFontOfSize:14],
                                    NSForegroundColorAttributeName  : [UIColor blackColor]}];

// Make '@' symbols red

[textStorage setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} 

// Make '#' symbols green

[textStorage setAttributes:@{NSForegroundColorAttributeName : [UIColor greenColor]} 

NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
[textStorage addLayoutManager:layoutManager];
NSTextContainer *container = [[NSTextContainer alloc] initWithSize:self.view.bounds.size];
[layoutManager addTextContainer:container];

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectZero textContainer:container];
[self.view addSubview:textView];