• Stars
    star
    154
  • Rank 242,095 (Top 5 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created over 11 years ago
  • Updated almost 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Easier way to create an attributes dictionary for NSAttributedString

MTStringAttributes

An easier way to create an attributes dictionary for NSAttributedString (for iOS and OSX)

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];     //or NSColor in OSX
attributes.backgroundColor  = [UIColor blackColor];   //or NSColor in OSX
attributes.strikethrough    = YES;
attributes.underline        = YES;

Some more advanced stuff

attributes.ligatures        = YES;
attributes.kern             = 1.0f;
attributes.outlineColor     = [UIColor blueColor];   //or NSColor in OSX
attributes.outlineWidth     = 2.0f;

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:

Easily create a string attributes object

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

Add this as the default for the whole string we're about to parse

[[MTStringParser sharedParser] setDefaultAttributes:attributes];

Define a style for a tag called <relative-time> that uses this font and has this color:

[[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]];

And easily add another tag that has a font, color, background color and is underlined:

[[MTStringParser sharedparser] addStyleWithTagName:@"em"
                                              font:[UIFont systemFontOfSize:14]
                                             color:[UIColor whiteColor]
                                   backgroundColor:[UIColor blackColor]
                                     strikethrough:NO
                                         underline:YES];

Now write the markup using the tags you defined styles for:

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];

And boom, your attributed string:

NSAttributedString *attributedString = [[MTStringParser sharedParser] attributedStringFromMarkup:markup];

Contributing

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

Author

Adam Kirk (@atomkirk)

More Repositories

1

MTMigration

Manages blocks of code that only need to run once on version updates in iOS apps.
Objective-C
913
star
2

MTAnimation

Animate UIView with 25+ timing functions. (Bounce, elastic, exponential, etc.)
Objective-C
505
star
3

MTDates

A thread safe date calculation library with all the date functions you'll ever need.
Objective-C
343
star
4

MTGeometry

An extension to Core Graphics Geometry. Intersections, scaling, etc.
Objective-C
212
star
5

MTPDF

Objective-C PDF objects. Doing my part to help us stay out of the headache that is Core Foundation.
Objective-C
205
star
6

MTControl

Attach block handlers to UIControl objects jQuery style
Objective-C
114
star
7

MTQueue

Add blocks to queues in a super terse and readable way!
Objective-C
61
star
8

MYSCategoryProperties

Quickly add properties to categories using runtime associations.
Objective-C
60
star
9

MTPencil

Draw animated lines (like an invisible pencil) with Core Graphics. (Prototype, will get much better).
Objective-C
47
star
10

MTFittedScrollView

A UIScrollView subclass that resizes itself to fit around its content.
Objective-C
39
star
11

MYSForms

Easy forms for iOS
Objective-C
33
star
12

MYSRuntime

Obj-C Library that makes runtime self-inspection and class modification dead easy.
Objective-C
33
star
13

MTColorDistance

A category on UIColor. Pass in an array of colors and it will return the closest color to the receiver.
Objective-C
24
star
14

MTPocket

Cocoa Web Requests
Objective-C
23
star
15

EKEventToiCal

Returns a string in iCal format for an EKEvent object
Objective-C
22
star
16

MTBlockAlertView

An iOS Alert View that uses block-based delegation instead of protocols.
Objective-C
22
star
17

FatSecretKit

iOS client library for the FatSecret API
Objective-C
21
star
18

MYSSharedSettings

Access local settings and settings shared via iCloud with properties.
Objective-C
20
star
19

MTDateComponentsPicker

A picker for choosing date components. Allows some components to be undefined.
Objective-C
15
star
20

FakeContacts

An XCode project you can build onto the simulator or device to generate any number or random contacts for app testing
Objective-C
13
star
21

MYSCoreText

An Objective-C wrapper around Apple's Core Text framework.
Objective-C
11
star
22

MYSDynamicAlertView

An alert view that uses UIKit Dynamics to present a fun/interactive way to respond to alerts.
Objective-C
9
star
23

flunk

A gem for testing Ruby on Rails web APIs by simulating a client.
Ruby
6
star
24

MYSShared

A submodule that includes objc code that will more than likely be used in any objc project we do.
Objective-C
3
star
25

MTTimer

An Objective-C timer that restricts firing to a time range. If it's called BEFORE min, it waits for min. If it's called AFTER max, it's called at max.
Objective-C
3
star
26

MYSGravityActionSheet

An action sheet that uses UIKit Dynamics to present buttons in a playful, interesting way.
Objective-C
2
star
27

MYSTableView

Objective-C
1
star
28

Pocket

A curl gem that doesn't suck
Ruby
1
star