Git Product home page Git Product logo

mtstringattributes's Introduction

MTStringAttributes

An easier way to create an attributes dictionary for NSAttributedString

Installation

In your Podfile, add this line:

pod "MTStringAttributes"

pod? => https://github.com/CocoaPods/CocoaPods/

Example Usage

#include <MTStringAttributes.h>

Create an attributes object

MTStringAttributes *attributes = [[MTStringAttributes alloc] init];

Set some basic properties

attributes.font             = nil;
attributes.textColor        = [UIColor redColor];
attributes.backgroundColor  = [UIColor blackColor];
attributes.strikethrough    = YES;
attributes.underline        = YES;

Some more advanced stuff

attributes.ligatures        = YES;
attributes.kern             = @(1);
attributes.outlineColor     = [UIColor blueColor];
attributes.outlineWidth     = @(2);

I might break out the properties for paragraph style later, but for now, provide an object

NSParagraphStyle *ps        = [[NSParagraphStyle alloc] init]
ps.alignment                = NSTextAlignmentLeft;
attributes.paragraphStyle   = ps;

Shadow

attributes.shadowBlurRadius = @(1.4);
attributes.shadowColor      = [UIColor grayColor];
attributes.shadowOffsetX    = @(0.2);
attributes.shadowOffsetY    = @(0.3);

Finally

NSAttributedString *str     = [[NSAttributedString alloc] initWithString:@"The attributed string!"
                                                              attributes:[attributes dictionary]];

Parser

Relying on Slash, MTStringParser allows you to add styles to tags and then generate attributed strings from markup of those tags.

#include <MTStringParser.h>

[[MTStringParser sharedParser] addStyleWithTagName:@"red"
                                              font:[UIFont systemFontOfSize:12]
                                             color:[UIColor redColor]];

NSAttributedString *string = [[MTStringParser sharedParser]
                                attributedStringFromMarkup:@"This is a <red>red section</red>"];

And like a beautiful symphony, they work together like so:

MTStringAttributes *attributes = [[MTStringAttributes alloc] init];
attributes.font = [UIFont fontWithName:@"HelveticaNeue" size:14];
attributes.textColor = [UIColor blackColor];

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.firstLineHeadIndent = 15;
attributes.paragraphStyle = paragraphStyle;

[[MTStringParser sharedParser] setDefaultAttributes:attributes];
[[MTStringParser sharedParser] addStyleWithTagName:@"relative-time"
                                              font:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14]
                                             color:[UIColor colorWithRed:215.0/255.0 green:0 blue:0 alpha:1]];

- (void)addStyleWithTagName:@"em"
                       font:[UIFont systemFontOfSize:14]
                      color:[UIColor whiteColor]
            backgroundColor:[UIColor blackColor]
              strikethrough:NO
                  underline:YES;

NSString *markup = [NSString stringWithFormat:@"You can have a <em>complex<em> string that  \
uses <em>tags</em> to define where you want <em>styles</em> to be defined. You needed       \
this <relative-time>%@</relative-time>.", timeAgo];

NSAttributedString *attributedString = [[MTStringParser sharedParser]
                                            attributedStringFromMarkup:@"This is a <red>red section</red>"];

Contributing

Please update and run the tests before submitting a pull request. Thanks.

Author

Adam Kirk (@atomkirk)

mtstringattributes's People

Contributors

atomkirk avatar

Watchers

Fred Chen avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.