forked from Cocoanetics/DTCoreText
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSAttributedString+HTML.m
More file actions
275 lines (215 loc) · 7.71 KB
/
Copy pathNSAttributedString+HTML.m
File metadata and controls
275 lines (215 loc) · 7.71 KB
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
//
// NSAttributedString+HTML.m
// DTCoreText
//
// Created by Oliver Drobnik on 1/9/11.
// Copyright 2011 Drobnik.com. All rights reserved.
//
#if TARGET_OS_IPHONE
#elif TARGET_OS_MAC
#import <ApplicationServices/ApplicationServices.h>
#endif
#import "DTHTMLElement.h"
#import "DTCoreTextConstants.h"
#import "DTHTMLAttributedStringBuilder.h"
#import "DTTextAttachment.h"
#if TARGET_OS_IPHONE
#import "NSAttributedStringRunDelegates.h"
#endif
@implementation NSAttributedString (HTML)
- (id)initWithHTMLData:(NSData *)data documentAttributes:(NSDictionary * __autoreleasing*)docAttributes
{
return [self initWithHTMLData:data options:nil documentAttributes:docAttributes];
}
- (id)initWithHTMLData:(NSData *)data baseURL:(NSURL *)base documentAttributes:(NSDictionary * __autoreleasing*)docAttributes
{
NSDictionary *optionsDict = nil;
if (base)
{
optionsDict = [NSDictionary dictionaryWithObject:base forKey:NSBaseURLDocumentOption];
}
return [self initWithHTMLData:data options:optionsDict documentAttributes:docAttributes];
}
- (id)initWithHTMLData:(NSData *)data options:(NSDictionary *)options documentAttributes:(NSDictionary * __autoreleasing*)docAttributes
{
// only with valid data
if (![data length])
{
return nil;
}
DTHTMLAttributedStringBuilder *stringBuilder = [[DTHTMLAttributedStringBuilder alloc] initWithHTML:data options:options documentAttributes:docAttributes];
void (^callBackBlock)(DTHTMLElement *element) = [options objectForKey:DTWillFlushBlockCallBack];
if (callBackBlock)
{
[stringBuilder setWillFlushCallback:callBackBlock];
}
// This needs to be on a separate line so that ARC can handle releasing the object properly
// return [stringBuilder generatedAttributedString]; shows leak in instruments
id string = [stringBuilder generatedAttributedString];
return string;
}
#pragma mark - NSAttributedString Archiving
+ (NSMutableDictionary *)getArchivingDictionaryWith:(NSDictionary *)attrs
{
NSDictionary *archiveDict = attrs[DTArchivingAttribute];
NSMutableDictionary *dict = nil;
if (![archiveDict isKindOfClass:[NSDictionary class]])
{
dict = [NSMutableDictionary dictionary];
}
else
{
dict = [archiveDict mutableCopy];
}
return dict;
}
- (NSData *)convertToData
{
NSMutableAttributedString *appendString = [self mutableCopy];
#if DTCORETEXT_SUPPORT_NS_ATTRIBUTES && TARGET_OS_IPHONE
NSUInteger length = [self length];
if (length)
{
[self enumerateAttributesInRange:NSMakeRange(0, length-1) options:0 usingBlock:^(NSDictionary<NSString *,id> * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) {
NSMutableDictionary *dict = [[self class] getArchivingDictionaryWith:attrs];
if (attrs[NSAttachmentAttributeName])
{
DTTextAttachment *attatchment = attrs[NSAttachmentAttributeName];
NSString *imgPath = nil;
if ([[attatchment.contentURL scheme] isEqualToString:@"file"])
{
imgPath = [attatchment.contentURL path];
NSUInteger homeDirLength = [NSHomeDirectory() length];
if ([imgPath hasPrefix:NSHomeDirectory()] && [imgPath length] > homeDirLength)
{
imgPath = [imgPath substringFromIndex:homeDirLength];
}
}
else
{
imgPath = [attatchment.contentURL absoluteString];
}
if (imgPath)
{
[dict setObject:imgPath forKey:NSAttachmentAttributeName];
[appendString addAttribute:DTArchivingAttribute value:dict range:range];
}
[appendString removeAttribute:(id)kCTRunDelegateAttributeName range:range];
}
// if there will others attribute to archiving , implement like this.
if (attrs[DTBackgroundStrokeColorAttribute])
{
CGColorRef strokeColor = (__bridge CGColorRef)(attrs[DTBackgroundStrokeColorAttribute]);
UIColor *stoke = [[UIColor alloc] initWithCGColor:strokeColor];
[dict setObject:stoke forKey:DTBackgroundStrokeColorAttribute];
[appendString addAttribute:DTArchivingAttribute value:dict range:range];
[appendString removeAttribute:(id)DTBackgroundStrokeColorAttribute range:range];
}
}];
}
#endif
NSData *data = nil;
@try
{
data = [NSKeyedArchiver archivedDataWithRootObject:appendString];
}
@catch (NSException *exception)
{
data = nil;
}
return data;
}
+ (NSAttributedString *)attributedStringWithData:(NSData *)data
{
NSMutableAttributedString *appendString = nil;
@try
{
appendString = (NSMutableAttributedString *)[NSKeyedUnarchiver unarchiveObjectWithData:data];
}
@catch (NSException *exception)
{
appendString = nil;
}
NSUInteger length = [appendString length];
if (length)
{
[appendString enumerateAttributesInRange:NSMakeRange(0, length-1) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(NSDictionary<NSString *,id> * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) {
#if DTCORETEXT_SUPPORT_NS_ATTRIBUTES && TARGET_OS_IPHONE
if (attrs[NSAttachmentAttributeName])
{
DTTextAttachment *attatchment = attrs[NSAttachmentAttributeName];
if ([[attatchment.contentURL scheme] isEqualToString:@"file"])
{
NSMutableDictionary *dict = [[self class] getArchivingDictionaryWith:attrs];
NSString *imgPath = dict[NSAttachmentAttributeName];
if (imgPath)
{
if (![imgPath hasPrefix:NSHomeDirectory()])
{
imgPath = [NSHomeDirectory() stringByAppendingPathComponent:imgPath];
}
attatchment.contentURL = [NSURL fileURLWithPath:imgPath];
}
}
CTRunDelegateRef embeddedObjectRunDelegate = createEmbeddedObjectRunDelegate(attatchment);
[appendString addAttribute:(id)kCTRunDelegateAttributeName value:CFBridgingRelease(embeddedObjectRunDelegate) range:range];
}
// if there will others attribute to archiving , implement like this.
if (attrs[DTBackgroundStrokeColorAttribute])
{
NSMutableDictionary *dict = [self getArchivingDictionaryWith:attrs];
UIColor *stroke = dict[DTBackgroundStrokeColorAttribute];
CGColorRef strokeColor = stroke.CGColor;
[appendString addAttribute:DTBackgroundStrokeColorAttribute value:(__bridge id)strokeColor range:range];
}
#endif
}];
}
return [appendString copy];
}
#pragma mark - Working with Custom HTML Attributes
- (NSDictionary *)HTMLAttributesAtIndex:(NSUInteger)index
{
return [self attribute:DTCustomAttributesAttribute atIndex:index effectiveRange:NULL];
}
- (NSRange)rangeOfHTMLAttribute:(NSString *)name atIndex:(NSUInteger)index
{
NSRange rangeSoFar;
NSDictionary *attributes = [self attribute:DTCustomAttributesAttribute atIndex:index effectiveRange:&rangeSoFar];
NSAssert(attributes, @"No custom attribute '%@' at index %d", name, (int)index);
// check if there is a value for this custom attribute name
id value = [attributes objectForKey:name];
if (!attributes || !value)
{
return NSMakeRange(NSNotFound, 0);
}
// search towards beginning
while (rangeSoFar.location>0)
{
NSRange extendedRange;
attributes = [self attribute:DTCustomAttributesAttribute atIndex:rangeSoFar.location-1 effectiveRange:&extendedRange];
id extendedValue = [attributes objectForKey:name];
// abort search if key not found or value not identical
if (!extendedValue || ![extendedValue isEqual:value])
{
break;
}
rangeSoFar = NSUnionRange(rangeSoFar, extendedRange);
}
NSUInteger length = [self length];
// search towards end
while (NSMaxRange(rangeSoFar)<length)
{
NSRange extendedRange;
attributes = [self attribute:DTCustomAttributesAttribute atIndex:NSMaxRange(rangeSoFar) effectiveRange:&extendedRange];
id extendedValue = [attributes objectForKey:name];
// abort search if key not found or value not identical
if (!extendedValue || ![extendedValue isEqual:value])
{
break;
}
rangeSoFar = NSUnionRange(rangeSoFar, extendedRange);
}
return rangeSoFar;
}
@end