Permalink
Browse files

Various clean up work

- renamed initWithHTML to initWithHTMLData to avoid clash with (future) private function
- Moved flush callback into option to get it out of the initWithHTMLData
- Added AppleDoc comments to NSAttributedString+HTML.h
  • Loading branch information...
1 parent d25a270 commit 00d135f99193fc5dbcc69eb07b4bcaa0f4a71b0b @odrobnik odrobnik committed Jun 21, 2012
@@ -115,7 +115,7 @@ - (void)setHTMLString:(NSString *)html
_htmlHash = newHash;
NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];
- NSAttributedString *string = [[NSAttributedString alloc] initWithHTML:data documentAttributes:NULL];
+ NSAttributedString *string = [[NSAttributedString alloc] initWithHTMLData:data documentAttributes:NULL];
self.attributedString = string;
}
@@ -5,10 +5,12 @@
// standard options
+#if TARGET_OS_IPHONE
extern NSString * const NSBaseURLDocumentOption;
extern NSString * const NSTextEncodingNameDocumentOption;
extern NSString * const NSTextSizeMultiplierDocumentOption;
extern NSString * const NSAttachmentAttributeName;
+#endif
// custom options
@@ -2,10 +2,12 @@
// standard options
+#if TARGET_OS_IPHONE
NSString * const NSBaseURLDocumentOption = @"NSBaseURLDocumentOption";
NSString * const NSTextEncodingNameDocumentOption = @"NSTextEncodingNameDocumentOption";
NSString * const NSTextSizeMultiplierDocumentOption = @"NSTextSizeMultiplierDocumentOption";
NSString * const NSAttachmentAttributeName = @"NSAttachmentAttributeName";
+#endif
// custom options
@@ -8,10 +8,40 @@
@class NSAttributedString;
+/**
+ Methods for generating an `NSAttributedString` from HTML data. Those methods exist on Mac but have not been ported (publicly) to iOS. This project aims to remedy this.
+ */
+
@interface NSAttributedString (HTML)
-- (id)initWithHTML:(NSData *)data documentAttributes:(NSDictionary **)dict;
-- (id)initWithHTML:(NSData *)data baseURL:(NSURL *)base documentAttributes:(NSDictionary **)dict;
-- (id)initWithHTML:(NSData *)data options:(NSDictionary *)options documentAttributes:(NSDictionary **)dict;
+/**
+ @name Creating an NSAttributedString
+ */
+
+/**
+ Initializes and returns a new `NSAttributedString` object from the HTML contained in the given object and base URL.
+ @param data The data in HTML format from which to create the attributed string.
+ @param docAttributes Currently not in used.
+ @returns Returns an initialized object, or nil if the data can’t be decoded.
+ */
+- (id)initWithHTMLData:(NSData *)data documentAttributes:(NSDictionary **)docAttributes;
+
+/**
+ Initializes and returns a new `NSAttributedString` object from the HTML contained in the given object and base URL.
+ @param data The data in HTML format from which to create the attributed string.
+ @param baseURL An `NSURL` that represents the base URL for all links within the HTML.
+ @param docAttributes Currently not in used.
+ @returns Returns an initialized object, or nil if the data can’t be decoded.
+ */
+- (id)initWithHTMLData:(NSData *)data baseURL:(NSURL *)baseURL documentAttributes:(NSDictionary **)docAttributes;
+
+/**
+ Initializes and returns a new `NSAttributedString` object from the HTML contained in the given object and base URL.
+ @param data The data in HTML format from which to create the attributed string.
+ @param baseURL An `NSURL` that represents the base URL for all links within the HTML.
+ @param docAttributes Currently not in used.
+ @returns Returns an initialized object, or nil if the data can’t be decoded.
+ */
+- (id)initWithHTMLData:(NSData *)data options:(NSDictionary *)options documentAttributes:(NSDictionary **)docAttributes;
@end
@@ -12,39 +12,16 @@
#import <ApplicationServices/ApplicationServices.h>
#endif
-#import "DTCoreTextConstants.h"
-
-#import "NSAttributedString+HTML.h"
-#import "NSMutableAttributedString+HTML.h"
-
-#import "NSString+HTML.h"
-#import "DTColor+HTML.h"
-#import "NSScanner+HTML.h"
-#import "NSCharacterSet+HTML.h"
-#import "DTTextAttachment.h"
-
-#import "DTHTMLElement.h"
-#import "DTCSSListStyle.h"
-#import "DTCSSStylesheet.h"
-
-#import "DTCoreTextFontDescriptor.h"
-#import "DTCoreTextParagraphStyle.h"
-
-#import "CGUtils.h"
-#import "NSString+UTF8Cleaner.h"
-#import "DTCoreTextConstants.h"
-#import "DTHTMLAttributedStringBuilder.h"
-
-
+#import "DTCoreText.h"
@implementation NSAttributedString (HTML)
-- (id)initWithHTML:(NSData *)data documentAttributes:(NSDictionary **)dict
+- (id)initWithHTMLData:(NSData *)data documentAttributes:(NSDictionary **)docAttributes
{
- return [self initWithHTML:data options:nil documentAttributes:dict];
+ return [self initWithHTMLData:data options:nil documentAttributes:docAttributes];
}
-- (id)initWithHTML:(NSData *)data baseURL:(NSURL *)base documentAttributes:(NSDictionary **)dict
+- (id)initWithHTMLData:(NSData *)data baseURL:(NSURL *)base documentAttributes:(NSDictionary **)docAttributes
{
NSDictionary *optionsDict = nil;
@@ -53,30 +30,25 @@ - (id)initWithHTML:(NSData *)data baseURL:(NSURL *)base documentAttributes:(NSDi
optionsDict = [NSDictionary dictionaryWithObject:base forKey:NSBaseURLDocumentOption];
}
- return [self initWithHTML:data options:optionsDict documentAttributes:dict];
+ return [self initWithHTMLData:data options:optionsDict documentAttributes:docAttributes];
}
-- (id)initWithHTML:(NSData *)data options:(NSDictionary *)options documentAttributes:(NSDictionary **)dict
+- (id)initWithHTMLData:(NSData *)data options:(NSDictionary *)options documentAttributes:(NSDictionary **)docAttributes
{
// only with valid data
if (![data length])
{
-
return nil;
}
- DTHTMLAttributedStringBuilder *stringBuilder = [[DTHTMLAttributedStringBuilder alloc] initWithHTML:data options:options documentAttributes:dict];
+ DTHTMLAttributedStringBuilder *stringBuilder = [[DTHTMLAttributedStringBuilder alloc] initWithHTML:data options:options documentAttributes:docAttributes];
- // example for setting a willFlushCallback, that gets called before elements are written to the generated attributed string
+ void (^callBackBlock)(DTHTMLElement *element) = [options objectForKey:DTWillFlushBlockCallBack];
- [stringBuilder setWillFlushCallback:^(DTHTMLElement *element)
+ if (callBackBlock)
{
- // if an element is larger than twice the font size put it in it's own block
- if (element.displayStyle == DTHTMLElementDisplayStyleInline && element.textAttachment.displaySize.height > 2.0 * element.fontDescriptor.pointSize)
- {
- element.displayStyle = DTHTMLElementDisplayStyleBlock;
- }
- } ];
+ [stringBuilder setWillFlushCallback:callBackBlock];
+ }
[stringBuilder buildString];
@@ -18,7 +18,7 @@ @implementation NSAttributedStringHTMLTest
- (NSAttributedString *)attributedStringFromHTML:(NSString *)html
{
NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];
- DTHTMLAttributedStringBuilder *stringBuilder = [[DTHTMLAttributedStringBuilder alloc] initWithHTML:data options:nil documentAttributes:NULL];
+ DTHTMLAttributedStringBuilder*stringBuilder = [[DTHTMLAttributedStringBuilder alloc] initWithHTML:data options:nil documentAttributes:NULL];
[stringBuilder buildString];
@@ -106,10 +106,19 @@ - (void)viewDidLoad {
// Create attributed string from HTML
CGSize maxImageSize = CGSizeMake(self.view.bounds.size.width - 20.0, self.view.bounds.size.height - 20.0);
+ // example for setting a willFlushCallback, that gets called before elements are written to the generated attributed string
+ void (^callBackBlock)(DTHTMLElement *element) = ^(DTHTMLElement *element) {
+ // if an element is larger than twice the font size put it in it's own block
+ if (element.displayStyle == DTHTMLElementDisplayStyleInline && element.textAttachment.displaySize.height > 2.0 * element.fontDescriptor.pointSize)
+ {
+ element.displayStyle = DTHTMLElementDisplayStyleBlock;
+ }
+ };
+
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:1.0], NSTextSizeMultiplierDocumentOption, [NSValue valueWithCGSize:maxImageSize], DTMaxImageSize,
- @"Times New Roman", DTDefaultFontFamily, @"purple", DTDefaultLinkColor, baseURL, NSBaseURLDocumentOption, nil];
+ @"Times New Roman", DTDefaultFontFamily, @"purple", DTDefaultLinkColor, baseURL, NSBaseURLDocumentOption, callBackBlock, DTWillFlushBlockCallBack, nil];
- NSAttributedString *string = [[NSAttributedString alloc] initWithHTML:data options:options documentAttributes:NULL];
+ NSAttributedString *string = [[NSAttributedString alloc] initWithHTMLData:data options:options documentAttributes:NULL];
// Display string
_textView.contentView.edgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);

0 comments on commit 00d135f

Please sign in to comment.