/
NSAttributedString+OHAdditions.m
298 lines (249 loc) · 10.3 KB
/
NSAttributedString+OHAdditions.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*******************************************************************************
* This software is under the MIT License quoted below:
*******************************************************************************
*
* Copyright (c) 2010 Olivier Halligon
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
******************************************************************************/
#import "NSAttributedString+OHAdditions.h"
@implementation NSAttributedString (OHAdditions)
/******************************************************************************/
#pragma mark - Convenience Constructors
+ (instancetype)attributedStringWithString:(NSString*)string
{
if (string)
{
return [[self alloc] initWithString:string];
}
else
{
return [self new];
}
}
+ (instancetype)attributedStringWithFormat:(NSString*)format, ...
{
va_list args;
va_start(args, format);
NSString* string = [[NSString alloc] initWithFormat:format arguments:args];
va_end(args);
return [[self alloc] initWithString:string];
}
+ (instancetype)attributedStringWithAttributedString:(NSAttributedString*)attrStr
{
if (attrStr)
{
return [[self alloc] initWithAttributedString:attrStr];
}
else
{
return [self new];
}
}
+ (instancetype)attributedStringWithHTML:(NSString*)htmlString
{
NSData* htmlData = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
if (htmlData)
{
NSDictionary* options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)};
__block id attributedString = nil;
dispatch_block_t block = ^{
attributedString = [[self alloc] initWithData:htmlData
options:options
documentAttributes:nil
error:NULL];
};
if ([NSThread isMainThread])
{
block();
}
else
{
// See Apple Doc: HTML importer should always be called on the main thread
dispatch_sync(dispatch_get_main_queue(), block);
}
return attributedString;
}
else
{
return nil;
}
}
+ (void)loadHTMLString:(NSString*)htmlString
completion:(void(^)(NSAttributedString* attrString))completion
{
if (!completion) return;
NSData* htmlData = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
if (htmlData)
{
NSDictionary* options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)};
dispatch_async(dispatch_get_main_queue(), ^{
NSAttributedString* attributedString = [[self alloc] initWithData:htmlData
options:options
documentAttributes:nil
error:NULL];
completion(attributedString);
});
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
completion(nil);
});
}
}
/******************************************************************************/
#pragma mark - Size
- (CGSize)sizeConstrainedToSize:(CGSize)maxSize
{
// Use NSStringDrawingUsesLineFragmentOrigin to compute bounds of multi-line strings (see Apple doc)
CGRect bounds = [self boundingRectWithSize:maxSize
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
// We need to ceil the returned values (see Apple doc)
return CGSizeMake((CGFloat)ceil((double)bounds.size.width),
(CGFloat)ceil((double)bounds.size.height) );
}
/******************************************************************************/
#pragma mark - Text Font
+ (UIFont*)defaultFont
{
// The default font used for NSAttributedString according to the doc
return [UIFont fontWithName:@"Helvetica" size:12];
}
- (UIFont*)fontAtIndex:(NSUInteger)index effectiveRange:(NSRangePointer)aRange
{
return [self attribute:NSFontAttributeName atIndex:index effectiveRange:aRange];
}
- (void)enumerateFontsInRange:(NSRange)enumerationRange
includeUndefined:(BOOL)includeUndefined
usingBlock:(void (^)(UIFont*, NSRange, BOOL *))block
{
NSParameterAssert(block);
[self enumerateAttribute:NSFontAttributeName
inRange:enumerationRange
options:0
usingBlock:^(id font, NSRange aRange, BOOL *stop)
{
if (font || includeUndefined) block(font, aRange, stop);
}];
}
/******************************************************************************/
#pragma mark - Text Color
- (UIColor*)textColorAtIndex:(NSUInteger)index effectiveRange:(NSRangePointer)aRange
{
return [self attribute:NSForegroundColorAttributeName atIndex:index effectiveRange:aRange];
}
- (UIColor*)textBackgroundColorAtIndex:(NSUInteger)index effectiveRange:(NSRangePointer)aRange
{
return [self attribute:NSBackgroundColorAttributeName atIndex:index effectiveRange:aRange];
}
/******************************************************************************/
#pragma mark - Text Underlining
- (BOOL)isTextUnderlinedAtIndex:(NSUInteger)index effectiveRange:(NSRangePointer)aRange
{
NSUnderlineStyle underlineStyle = [self textUnderlineStyleAtIndex:index effectiveRange:aRange];
return underlineStyle != NSUnderlineStyleNone;
}
- (NSUnderlineStyle)textUnderlineStyleAtIndex:(NSUInteger)index effectiveRange:(NSRangePointer)aRange
{
NSNumber* attr = [self attribute:NSUnderlineStyleAttributeName atIndex:index effectiveRange:aRange];
return [attr integerValue];
}
- (UIColor*)textUnderlineColorAtIndex:(NSUInteger)index effectiveRange:(NSRangePointer)aRange
{
return [self attribute:NSUnderlineColorAttributeName atIndex:index effectiveRange:aRange];
}
/******************************************************************************/
#pragma mark - Text Style & Traits
- (BOOL)isFontBoldAtIndex:(NSUInteger)index effectiveRange:(NSRangePointer)aRange
{
UIFont* font = [self fontAtIndex:index effectiveRange:aRange];
UIFontDescriptorSymbolicTraits symTraits = font.fontDescriptor.symbolicTraits;
return (symTraits & UIFontDescriptorTraitBold) != 0;
}
- (BOOL)isFontItalicsAtIndex:(NSUInteger)index effectiveRange:(NSRangePointer)aRange
{
UIFont* font = [self fontAtIndex:index effectiveRange:aRange];
UIFontDescriptorSymbolicTraits symTraits = font.fontDescriptor.symbolicTraits;
return (symTraits & UIFontDescriptorTraitItalic) != 0;
}
/******************************************************************************/
#pragma mark - Links
- (NSURL*)URLAtIndex:(NSUInteger)index effectiveRange:(NSRangePointer)aRange
{
return [self attribute:NSLinkAttributeName atIndex:index effectiveRange:aRange];
}
- (void)enumerateURLsInRange:(NSRange)enumerationRange
usingBlock:(void (^)(NSURL*, NSRange, BOOL *))block
{
NSParameterAssert(block);
[self enumerateAttribute:NSLinkAttributeName
inRange:enumerationRange
options:0
usingBlock:^(id url, NSRange range, BOOL *stop)
{
if (url) block(url, range, stop);
}];
}
/******************************************************************************/
#pragma mark - Character Spacing
- (CGFloat)characterSpacingAtIndex:(NSUInteger)index effectiveRange:(NSRangePointer)aRange
{
return [[self attribute:NSKernAttributeName atIndex:index effectiveRange:aRange] floatValue];
}
/******************************************************************************/
#pragma mark - Subscript and Superscript
- (CGFloat)baselineOffsetAtIndex:(NSUInteger)index effectiveRange:(NSRangePointer)aRange
{
return [[self attribute:NSBaselineOffsetAttributeName atIndex:index effectiveRange:aRange] floatValue];
}
/******************************************************************************/
#pragma mark - Paragraph Style
- (NSTextAlignment)textAlignmentAtIndex:(NSUInteger)index effectiveRange:(NSRangePointer)aRange
{
NSParagraphStyle* style = [self paragraphStyleAtIndex:index effectiveRange:aRange];
return style.alignment;
}
- (NSLineBreakMode)lineBreakModeAtIndex:(NSUInteger)index effectiveRange:(NSRangePointer)aRange
{
NSParagraphStyle* style = [self paragraphStyleAtIndex:index effectiveRange:aRange];
return style.lineBreakMode;
}
- (NSParagraphStyle*)paragraphStyleAtIndex:(NSUInteger)index effectiveRange:(NSRangePointer)aRange
{
return [self attribute:NSParagraphStyleAttributeName atIndex:index effectiveRange:aRange];
}
- (void)enumerateParagraphStylesInRange:(NSRange)enumerationRange
includeUndefined:(BOOL)includeUndefined
usingBlock:(void (^)(NSParagraphStyle*, NSRange, BOOL *))block
{
NSParameterAssert(block);
[self enumerateAttribute:NSParagraphStyleAttributeName
inRange:enumerationRange
options:0
usingBlock:^(id style, NSRange aRange, BOOL *stop)
{
if (style || includeUndefined) block(style, aRange, stop);
}];
}
@end