Skip to content

Commit 110ed15

Browse files
committed
adding string parser
1 parent 1cb61ce commit 110ed15

35 files changed

Lines changed: 5665 additions & 18 deletions

MTStringAttributes.podspec

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
Pod::Spec.new do |s|
2-
s.name = "MTStringAttributes"
3-
s.version = "0.0.1"
4-
s.summary = "Makes it way easier to generate attribute dictionaries needed for NSAttributedString (on iOS)"
5-
s.homepage = "https://github.com/mysterioustrousers/MTStringAttributes"
6-
s.license = 'MIT'
7-
s.author = { "Adam Kirk" => "atomkirk@gmail.com" }
8-
s.source = { :git => "https://github.com/mysterioustrousers/MTStringAttributes.git", :tag => "0.0.1" }
9-
s.platform = :ios, '6.0'
10-
s.source_files = 'MTStringAttributes/*.{h,m}'
11-
s.frameworks = 'CoreGraphics', 'UIKit', 'Foundation'
12-
s.requires_arc = true
2+
s.name = "MTStringAttributes"
3+
s.version = "0.0.2"
4+
s.summary = "Makes it way easier to generate attribute dictionaries needed for NSAttributedString (on iOS)."
5+
s.homepage = "https://github.com/mysterioustrousers/MTStringAttributes"
6+
s.license = 'MIT'
7+
s.author = { "Adam Kirk" => "atomkirk@gmail.com" }
8+
s.source = { :git => "https://github.com/mysterioustrousers/MTStringAttributes.git", :tag => "0.0.2" }
9+
s.ios.deployment_target = '6.0'
10+
s.osx.deployment_target = '10.7'
11+
s.source_files = 'MTStringAttributes/*.{h,m}'
12+
s.frameworks = 'CoreGraphics', 'UIKit', 'Foundation'
13+
s.requires_arc = true
14+
s.dependency 'Slash', '~> 0.0.1'
1315
end

MTStringAttributes.xcodeproj/project.pbxproj

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
1C20BBFF1DC141389528F702 /* libPods.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A560F9CCC1D6477CB95BF43F /* libPods.a */; };
1011
C25AD64216CB37CC00488780 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C25AD64116CB37CC00488780 /* Foundation.framework */; };
1112
C25AD64716CB37CC00488780 /* MTStringAttributes.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = C25AD64616CB37CC00488780 /* MTStringAttributes.h */; };
1213
C25AD64916CB37CC00488780 /* MTStringAttributes.m in Sources */ = {isa = PBXBuildFile; fileRef = C25AD64816CB37CC00488780 /* MTStringAttributes.m */; };
@@ -18,6 +19,8 @@
1819
C25AD66A16CB37EF00488780 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C25AD66916CB37EF00488780 /* UIKit.framework */; };
1920
C25AD66C16CB564D00488780 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C25AD66B16CB564D00488780 /* CoreGraphics.framework */; };
2021
C25AD66D16CB5C4500488780 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C25AD66916CB37EF00488780 /* UIKit.framework */; };
22+
C25AD67016CB69AC00488780 /* MTStringParser.m in Sources */ = {isa = PBXBuildFile; fileRef = C25AD66F16CB69AC00488780 /* MTStringParser.m */; };
23+
C2B10ACC16CB721200FB4F5D /* MTStringParserTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C2B10ACB16CB721200FB4F5D /* MTStringParserTests.m */; };
2124
/* End PBXBuildFile section */
2225

2326
/* Begin PBXContainerItemProxy section */
@@ -44,6 +47,8 @@
4447
/* End PBXCopyFilesBuildPhase section */
4548

4649
/* Begin PBXFileReference section */
50+
189A984AEC744EA0B4187584 /* Pods.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.xcconfig; path = Pods/Pods.xcconfig; sourceTree = SOURCE_ROOT; };
51+
A560F9CCC1D6477CB95BF43F /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; };
4752
C25AD63E16CB37CC00488780 /* libMTStringAttributes.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libMTStringAttributes.a; sourceTree = BUILT_PRODUCTS_DIR; };
4853
C25AD64116CB37CC00488780 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
4954
C25AD64516CB37CC00488780 /* MTStringAttributes-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "MTStringAttributes-Prefix.pch"; sourceTree = "<group>"; };
@@ -57,6 +62,10 @@
5762
C25AD65F16CB37CC00488780 /* MTStringAttributesTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MTStringAttributesTests.m; sourceTree = "<group>"; };
5863
C25AD66916CB37EF00488780 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
5964
C25AD66B16CB564D00488780 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
65+
C25AD66E16CB69AC00488780 /* MTStringParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTStringParser.h; sourceTree = "<group>"; };
66+
C25AD66F16CB69AC00488780 /* MTStringParser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MTStringParser.m; sourceTree = "<group>"; };
67+
C2B10ACA16CB721200FB4F5D /* MTStringParserTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTStringParserTests.h; sourceTree = "<group>"; };
68+
C2B10ACB16CB721200FB4F5D /* MTStringParserTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MTStringParserTests.m; sourceTree = "<group>"; };
6069
/* End PBXFileReference section */
6170

6271
/* Begin PBXFrameworksBuildPhase section */
@@ -67,6 +76,7 @@
6776
C25AD64216CB37CC00488780 /* Foundation.framework in Frameworks */,
6877
C25AD66C16CB564D00488780 /* CoreGraphics.framework in Frameworks */,
6978
C25AD66A16CB37EF00488780 /* UIKit.framework in Frameworks */,
79+
1C20BBFF1DC141389528F702 /* libPods.a in Frameworks */,
7080
);
7181
runOnlyForDeploymentPostprocessing = 0;
7282
};
@@ -91,6 +101,7 @@
91101
C25AD65816CB37CC00488780 /* MTStringAttributesTests */,
92102
C25AD64016CB37CC00488780 /* Frameworks */,
93103
C25AD63F16CB37CC00488780 /* Products */,
104+
189A984AEC744EA0B4187584 /* Pods.xcconfig */,
94105
);
95106
sourceTree = "<group>";
96107
};
@@ -110,13 +121,16 @@
110121
C25AD64116CB37CC00488780 /* Foundation.framework */,
111122
C25AD65016CB37CC00488780 /* SenTestingKit.framework */,
112123
C25AD66916CB37EF00488780 /* UIKit.framework */,
124+
A560F9CCC1D6477CB95BF43F /* libPods.a */,
113125
);
114126
name = Frameworks;
115127
sourceTree = "<group>";
116128
};
117129
C25AD64316CB37CC00488780 /* MTStringAttributes */ = {
118130
isa = PBXGroup;
119131
children = (
132+
C25AD66E16CB69AC00488780 /* MTStringParser.h */,
133+
C25AD66F16CB69AC00488780 /* MTStringParser.m */,
120134
C25AD64616CB37CC00488780 /* MTStringAttributes.h */,
121135
C25AD64816CB37CC00488780 /* MTStringAttributes.m */,
122136
C25AD64416CB37CC00488780 /* Supporting Files */,
@@ -137,6 +151,8 @@
137151
children = (
138152
C25AD65E16CB37CC00488780 /* MTStringAttributesTests.h */,
139153
C25AD65F16CB37CC00488780 /* MTStringAttributesTests.m */,
154+
C2B10ACA16CB721200FB4F5D /* MTStringParserTests.h */,
155+
C2B10ACB16CB721200FB4F5D /* MTStringParserTests.m */,
140156
C25AD65916CB37CC00488780 /* Supporting Files */,
141157
);
142158
path = MTStringAttributesTests;
@@ -161,6 +177,7 @@
161177
C25AD63A16CB37CC00488780 /* Sources */,
162178
C25AD63B16CB37CC00488780 /* Frameworks */,
163179
C25AD63C16CB37CC00488780 /* CopyFiles */,
180+
2E593D2A81454ADDA94FD44D /* Copy Pods Resources */,
164181
);
165182
buildRules = (
166183
);
@@ -229,6 +246,20 @@
229246
/* End PBXResourcesBuildPhase section */
230247

231248
/* Begin PBXShellScriptBuildPhase section */
249+
2E593D2A81454ADDA94FD44D /* Copy Pods Resources */ = {
250+
isa = PBXShellScriptBuildPhase;
251+
buildActionMask = 2147483647;
252+
files = (
253+
);
254+
inputPaths = (
255+
);
256+
name = "Copy Pods Resources";
257+
outputPaths = (
258+
);
259+
runOnlyForDeploymentPostprocessing = 0;
260+
shellPath = /bin/sh;
261+
shellScript = "\"${SRCROOT}/Pods/Pods-resources.sh\"\n";
262+
};
232263
C25AD64D16CB37CC00488780 /* ShellScript */ = {
233264
isa = PBXShellScriptBuildPhase;
234265
buildActionMask = 2147483647;
@@ -250,6 +281,7 @@
250281
buildActionMask = 2147483647;
251282
files = (
252283
C25AD64916CB37CC00488780 /* MTStringAttributes.m in Sources */,
284+
C25AD67016CB69AC00488780 /* MTStringParser.m in Sources */,
253285
);
254286
runOnlyForDeploymentPostprocessing = 0;
255287
};
@@ -258,6 +290,7 @@
258290
buildActionMask = 2147483647;
259291
files = (
260292
C25AD66016CB37CC00488780 /* MTStringAttributesTests.m in Sources */,
293+
C2B10ACC16CB721200FB4F5D /* MTStringParserTests.m in Sources */,
261294
);
262295
runOnlyForDeploymentPostprocessing = 0;
263296
};
@@ -338,23 +371,23 @@
338371
};
339372
C25AD66416CB37CC00488780 /* Debug */ = {
340373
isa = XCBuildConfiguration;
374+
baseConfigurationReference = 189A984AEC744EA0B4187584 /* Pods.xcconfig */;
341375
buildSettings = {
342376
DSTROOT = /tmp/MTStringAttributes.dst;
343377
GCC_PRECOMPILE_PREFIX_HEADER = YES;
344378
GCC_PREFIX_HEADER = "MTStringAttributes/MTStringAttributes-Prefix.pch";
345-
OTHER_LDFLAGS = "-ObjC";
346379
PRODUCT_NAME = "$(TARGET_NAME)";
347380
SKIP_INSTALL = YES;
348381
};
349382
name = Debug;
350383
};
351384
C25AD66516CB37CC00488780 /* Release */ = {
352385
isa = XCBuildConfiguration;
386+
baseConfigurationReference = 189A984AEC744EA0B4187584 /* Pods.xcconfig */;
353387
buildSettings = {
354388
DSTROOT = /tmp/MTStringAttributes.dst;
355389
GCC_PRECOMPILE_PREFIX_HEADER = YES;
356390
GCC_PREFIX_HEADER = "MTStringAttributes/MTStringAttributes-Prefix.pch";
357-
OTHER_LDFLAGS = "-ObjC";
358391
PRODUCT_NAME = "$(TARGET_NAME)";
359392
SKIP_INSTALL = YES;
360393
};
@@ -409,6 +442,7 @@
409442
C25AD66516CB37CC00488780 /* Release */,
410443
);
411444
defaultConfigurationIsVisible = 0;
445+
defaultConfigurationName = Release;
412446
};
413447
C25AD66616CB37CC00488780 /* Build configuration list for PBXNativeTarget "MTStringAttributesTests" */ = {
414448
isa = XCConfigurationList;
@@ -417,6 +451,7 @@
417451
C25AD66816CB37CC00488780 /* Release */,
418452
);
419453
defaultConfigurationIsVisible = 0;
454+
defaultConfigurationName = Release;
420455
};
421456
/* End XCConfigurationList section */
422457
};

MTStringAttributes/MTStringAttributes.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@
1010
@interface MTStringAttributes : NSObject
1111

1212
// Basics
13-
@property (strong, nonatomic) UIFont *font;
14-
@property (strong, nonatomic) UIColor *textColor;
15-
@property (strong, nonatomic) UIColor *backgroundColor;
13+
@property (strong, nonatomic) id font; // UIFont || NSFont
14+
@property (strong, nonatomic) id textColor; // UIColor || NSColor
15+
@property (strong, nonatomic) id backgroundColor; // UIColor || NSColor
1616
@property ( nonatomic) BOOL strikethrough;
1717
@property ( nonatomic) BOOL underline;
1818

1919
// Advanced
2020
@property ( nonatomic) BOOL ligatures; // Allow some characters to be combined
2121
@property (strong, nonatomic) NSNumber *kern; // distance between characters
22-
@property (strong, nonatomic) UIColor *outlineColor;
22+
@property (strong, nonatomic) id outlineColor; // UIColor || NSColor
2323
@property (strong, nonatomic) NSNumber *outlineWidth;
2424

2525
// Paragraph Style
2626
@property (strong, nonatomic) NSParagraphStyle *paragraphStyle;
2727

2828
// Shadow
2929
@property (strong, nonatomic) NSNumber *shadowBlurRadius;
30-
@property (strong, nonatomic) UIColor *shadowColor;
30+
@property (strong, nonatomic) id shadowColor; // UIColor || NSColor
3131
@property (strong, nonatomic) NSNumber *shadowOffsetX;
3232
@property (strong, nonatomic) NSNumber *shadowOffsetY;
3333

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// MTString.h
3+
// MTStringAttributes
4+
//
5+
// Created by Adam Kirk on 2/12/13.
6+
// Copyright (c) 2013 Mysterious Trousers. All rights reserved.
7+
//
8+
9+
#import "MTStringAttributes.h"
10+
11+
12+
@interface MTStringParser : NSObject
13+
14+
+ (MTStringParser *)sharedParser;
15+
16+
17+
#pragma mark - Set Styles
18+
19+
- (void)setDefaultAttributes:(MTStringAttributes *)attributes;
20+
21+
- (void)setAttributes:(MTStringAttributes *)attributes forTag:(NSString *)tag;
22+
23+
24+
#pragma mark - Convenience
25+
26+
- (void)addStyleWithTagName:(NSString *)tagName font:(id)font;
27+
28+
- (void)addStyleWithTagName:(NSString *)tagName
29+
font:(id)font
30+
color:(id)color;
31+
32+
- (void)addStyleWithTagName:(NSString *)tagName
33+
font:(id)font
34+
color:(id)color
35+
backgroundColor:(id)backgroundColor
36+
strikethrough:(BOOL)strikethrough
37+
underline:(BOOL)underline;
38+
39+
40+
#pragma mark - Parse Markup To Attributed String
41+
42+
- (NSAttributedString *)attributedStringFromMarkup:(NSString *)markup;
43+
44+
45+
@end
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//
2+
// MTString.m
3+
// MTStringAttributes
4+
//
5+
// Created by Adam Kirk on 2/12/13.
6+
// Copyright (c) 2013 Mysterious Trousers. All rights reserved.
7+
//
8+
9+
#import "MTStringParser.h"
10+
#import <SLSMarkupParser.h>
11+
12+
13+
@interface MTStringParser ()
14+
@property (strong, nonatomic) NSMutableDictionary *styles;
15+
@end
16+
17+
18+
@implementation MTStringParser
19+
20+
21+
- (id)init
22+
{
23+
self = [super init];
24+
if (self) {
25+
_styles = [NSMutableDictionary dictionary];
26+
}
27+
return self;
28+
}
29+
30+
+ (MTStringParser *)sharedParser
31+
{
32+
static MTStringParser *__parser = nil;
33+
static dispatch_once_t onceToken;
34+
dispatch_once(&onceToken, ^{
35+
__parser = [[MTStringParser alloc] init];
36+
});
37+
return __parser;
38+
}
39+
40+
41+
42+
#pragma mark - Set Styles
43+
44+
- (void)setDefaultAttributes:(MTStringAttributes *)attributes
45+
{
46+
_styles[@"$default"] = [attributes dictionary];
47+
}
48+
49+
- (void)setAttributes:(MTStringAttributes *)attributes forTag:(NSString *)tag
50+
{
51+
_styles[tag] = [attributes dictionary];
52+
}
53+
54+
55+
56+
#pragma mark - Convenience
57+
58+
- (void)addStyleWithTagName:(NSString *)tagName font:(id)font
59+
{
60+
MTStringAttributes *attributes = [[MTStringAttributes alloc] init];
61+
attributes.font = font;
62+
[self setAttributes:attributes forTag:tagName];
63+
}
64+
65+
- (void)addStyleWithTagName:(NSString *)tagName
66+
font:(id)font
67+
color:(id)color
68+
{
69+
MTStringAttributes *attributes = [[MTStringAttributes alloc] init];
70+
attributes.font = font;
71+
attributes.textColor = color;
72+
[self setAttributes:attributes forTag:tagName];
73+
}
74+
75+
- (void)addStyleWithTagName:(NSString *)tagName
76+
font:(id)font
77+
color:(id)color
78+
backgroundColor:(id)backgroundColor
79+
strikethrough:(BOOL)strikethrough
80+
underline:(BOOL)underline
81+
{
82+
MTStringAttributes *attributes = [[MTStringAttributes alloc] init];
83+
attributes.font = font;
84+
attributes.textColor = color;
85+
attributes.backgroundColor = backgroundColor;
86+
attributes.strikethrough = strikethrough;
87+
attributes.underline = underline;
88+
[self setAttributes:attributes forTag:tagName];
89+
}
90+
91+
92+
#pragma mark - Parse Markup To Attributed String
93+
94+
- (NSAttributedString *)attributedStringFromMarkup:(NSString *)markup
95+
{
96+
NSError *error = nil;
97+
NSAttributedString *string = [SLSMarkupParser attributedStringWithMarkup:markup style:_styles error:&error];
98+
if (error) NSLog(@"%@", [error localizedDescription]);
99+
return string;
100+
}
101+
102+
103+
@end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// MTStringParserTests.h
3+
// MTStringAttributes
4+
//
5+
// Created by Adam Kirk on 2/13/13.
6+
// Copyright (c) 2013 Mysterious Trousers. All rights reserved.
7+
//
8+
9+
#import <SenTestingKit/SenTestingKit.h>
10+
11+
@interface MTStringParserTests : SenTestCase
12+
13+
@end

0 commit comments

Comments
 (0)