-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNSMutableAttributedString+Unitytheory.m
242 lines (196 loc) · 6.04 KB
/
NSMutableAttributedString+Unitytheory.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//
// NSMutableAttributedString+Unitytheory.m
//
// Created by Danny Morrow on 3/6/15.
// Copyright (c) 2015 unitytheory. All rights reserved.
//
#import "NSMutableAttributedString+Unitytheory.h"
#import <CoreText/CTStringAttributes.h>
@implementation NSMutableAttributedString(unitytheory)
- (void) setMultiple:(NSDictionary *)properties
{
for (NSString* key in properties)
{
NSAssert([self respondsToSelector:NSSelectorFromString(key)], @"trying to set nonexistant property: '%@'", key);
//[self setAttribute:key value:properties[key]];
[self setValue:properties[key] forKey:key];
}
}
- (void) setAttribute:(NSString *)name value:(id)value
{
[self removeAttribute:name range:self.range];
[self addAttribute:name value:value range:self.range];
}
- (UIColor*) backgroundColor
{
return [self attribute:NSBackgroundColorAttributeName atIndex:0 effectiveRange:nil];
}
- (void) setBackgroundColor:(UIColor *)backgroundColor
{
[self setAttribute:NSBackgroundColorAttributeName value:backgroundColor];
}
- (CGFloat) baselineOffset
{
return [(NSNumber*)[self attribute:NSBaselineOffsetAttributeName atIndex:0 effectiveRange:nil] floatValue];
}
- (UIFont*) font
{
return [self attribute:NSFontAttributeName atIndex:0 effectiveRange:nil];
}
- (void) setFont:(UIFont *)font
{
[self setAttribute:NSFontAttributeName value:font];
}
- (void) setBaselineOffset:(CGFloat)baselineOffset
{
[self setAttribute:NSBaselineOffsetAttributeName value:@(baselineOffset)];
}
- (UIColor*) color
{
return [self attribute:NSForegroundColorAttributeName atIndex:0 effectiveRange:nil];
}
- (void) setColor:(UIColor *)color
{
[self setAttribute:NSForegroundColorAttributeName value:color];
}
- (CGFloat) kerning
{
return [(NSNumber*)[self attribute:NSKernAttributeName atIndex:0 effectiveRange:nil] floatValue];
}
- (void) setKerning:(CGFloat)kerning
{
[self setAttribute:NSKernAttributeName value:@(kerning)];
}
- (CGFloat) photoshopKerning
{
return self.kerning * 1000 / self.font.pointSize;
}
- (void) setPhotoshopKerning:(CGFloat)photoshopKerning
{
CGFloat pointSize = self.font.pointSize;
self.kerning = photoshopKerning / 1000 * pointSize;
}
- (NSUInteger) ligature
{
return [(NSNumber*)[self attribute:NSLigatureAttributeName atIndex:0 effectiveRange:nil] unsignedIntegerValue];
}
- (void) setLigature:(NSUInteger)ligature
{
[self setAttribute:NSLigatureAttributeName value:[NSNumber numberWithUnsignedInteger:ligature]];
}
- (NSURL*) link
{
return [self attribute:NSLinkAttributeName atIndex:0 effectiveRange:nil];
}
- (void) setLink:(NSURL *)link
{
[self setAttribute:NSLinkAttributeName value:link];
}
- (CGFloat) strokeWidth
{
return [(NSNumber*)[self attribute:NSStrokeWidthAttributeName atIndex:0 effectiveRange:nil] floatValue];
}
- (void) setStrokeWidth:(CGFloat)strokeWidth
{
[self setAttribute:NSStrokeWidthAttributeName value:@(strokeWidth)];
}
- (UIColor*) strokeColor
{
return [self attribute:NSStrokeColorAttributeName atIndex:0 effectiveRange:nil];
}
- (void) setStrokeColor:(UIColor *)strokeColor
{
[self setAttribute:NSStrokeColorAttributeName value:strokeColor];
}
- (NSInteger) superscript
{
return [(NSNumber*)[self attribute:(NSString*)kCTSuperscriptAttributeName atIndex:0 effectiveRange:nil] integerValue];
}
- (void) setSuperscript:(NSInteger)superscript
{
[self setAttribute:(NSString*)kCTSuperscriptAttributeName value:[NSNumber numberWithInteger:superscript]];
}
- (UIColor*) underlineColor
{
return [self attribute:NSUnderlineColorAttributeName atIndex:0 effectiveRange:nil];
}
- (void) setUnderlineColor:(UIColor *)underlineColor
{
[self setAttribute:NSUnderlineColorAttributeName value:underlineColor];
}
- (NSUnderlineStyle) underlineStyle
{
return (NSUnderlineStyle)[self attribute:NSUnderlineStyleAttributeName atIndex:0 effectiveRange:nil];
}
- (void) setUnderlineStyle:(NSUnderlineStyle)underlineStyle
{
[self setAttribute:NSUnderlineStyleAttributeName value:@(underlineStyle)];
}
- (CGFloat) lineSpacing
{
return self.paragraphStyle.lineSpacing;
}
- (void) setLineSpacing:(CGFloat)lineSpacing
{
NSMutableParagraphStyle* paragraphStyle = [self.paragraphStyle mutableCopy];
paragraphStyle.lineSpacing = lineSpacing;
self.paragraphStyle = paragraphStyle;
}
- (NSTextAlignment) alignment
{
return self.paragraphStyle.alignment;
}
- (void) setAlignment:(NSTextAlignment)alignment
{
NSMutableParagraphStyle* paragraphStyle = [self.paragraphStyle mutableCopy];
paragraphStyle.alignment = alignment;
self.paragraphStyle = paragraphStyle;
}
- (CGFloat) paragraphSpacing
{
return self.paragraphStyle.paragraphSpacing;
}
- (void) setParagraphSpacing:(CGFloat)paragraphSpacing
{
NSMutableParagraphStyle* paragraphStyle = [self.paragraphStyle mutableCopy];
paragraphStyle.paragraphSpacing = paragraphSpacing;
self.paragraphStyle = paragraphStyle;
}
- (CGFloat) lineHeightMultiple
{
return self.paragraphStyle.lineHeightMultiple;
}
- (void) setLineHeightMultiple:(CGFloat)lineHeightMultiple
{
NSMutableParagraphStyle* paragraphStyle = [self.paragraphStyle mutableCopy];
paragraphStyle.lineHeightMultiple = lineHeightMultiple;
self.paragraphStyle = paragraphStyle;
}
- (NSLineBreakMode) lineBreakMode
{
return self.paragraphStyle.lineBreakMode;
}
- (void) setLineBreakMode:(NSLineBreakMode)lineBreakMode
{
NSMutableParagraphStyle* paragraphStyle = [self.paragraphStyle mutableCopy];
paragraphStyle.lineBreakMode = lineBreakMode;
self.paragraphStyle = paragraphStyle;
}
- (NSRange) range
{
return NSMakeRange(0, self.length);
}
- (NSParagraphStyle*) paragraphStyle
{
NSParagraphStyle* existingParagraphStyle = (NSParagraphStyle*)[self attribute:NSParagraphStyleAttributeName atIndex:0 effectiveRange:nil];
if (existingParagraphStyle)
{
return [existingParagraphStyle mutableCopy];
}
return [NSParagraphStyle defaultParagraphStyle];
}
- (void) setParagraphStyle:(NSParagraphStyle *)paragraphStyle
{
[self setAttribute:NSParagraphStyleAttributeName value:paragraphStyle];
}
@end