Skip to content

Commit

Permalink
implemented Archiving
Browse files Browse the repository at this point in the history
  • Loading branch information
seedotlee committed Apr 23, 2017
1 parent 2cbb6db commit 0b34eda
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Core/Source/NSAttributedString+HTML.h
Expand Up @@ -72,4 +72,23 @@
*/
- (NSRange)rangeOfHTMLAttribute:(NSString *)name atIndex:(NSUInteger)index;

/**
Retrieves the NSAttributedString with NSData
Currently only supports iOS by `___useiOS6Attributes`, if error occur return nil.
@param data The data must generate by `convertToData` function
@return NSAttributedString from unarchiveObjectWithData, the data must generate by `convertToData` function
*/
+ (NSAttributedString *)attributedStringWithData:(NSData *)data;

/**
Retrieves NSData with self
Currently only supports iOS by `___useiOS6Attributes`, if error occur return nil.
@return NSData from NSAttributedString execute archivedDataWithRootObject:
*/
- (NSData *)convertToData;

@end
69 changes: 69 additions & 0 deletions Core/Source/NSAttributedString+HTML.m
Expand Up @@ -14,6 +14,11 @@
#import "DTHTMLElement.h"
#import "DTCoreTextConstants.h"
#import "DTHTMLAttributedStringBuilder.h"
#import "DTTextAttachment.h"

#if TARGET_OS_IPHONE
#import "NSAttributedStringRunDelegates.h"
#endif

@implementation NSAttributedString (HTML)

Expand Down Expand Up @@ -58,6 +63,70 @@ - (id)initWithHTMLData:(NSData *)data options:(NSDictionary *)options documentAt
return string;
}

#pragma mark - NSAttributedString Archiving

- (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) {

if (attrs[NSAttachmentAttributeName])
{
[appendString removeAttribute:(id)kCTRunDelegateAttributeName 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;
#if DTCORETEXT_SUPPORT_NS_ATTRIBUTES && TARGET_OS_IPHONE
@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 (attrs[NSAttachmentAttributeName])
{
DTTextAttachment *attatchment = attrs[NSAttachmentAttributeName];
CTRunDelegateRef embeddedObjectRunDelegate = createEmbeddedObjectRunDelegate(attatchment);

[appendString addAttribute:(id)kCTRunDelegateAttributeName value:CFBridgingRelease(embeddedObjectRunDelegate) range:range];
}

}];
}
#endif
return [appendString copy];
}

#pragma mark - Working with Custom HTML Attributes

- (NSDictionary *)HTMLAttributesAtIndex:(NSUInteger)index
Expand Down

0 comments on commit 0b34eda

Please sign in to comment.