HTML文字列をNSAttributedStringに解析する私の方法は、解析されたノード(およびそのchildNodes)をNSMutableAttributedStringに再帰的に追加することです。
まだ完全なコードをどこにも公開する準備ができていません。しかし、うまくいけば、これはあなたにいくつかのヒントを与えることができます...
NSString + HTML.h
/* - toHTMLElements
* parse the string itself into a dictionary collection of htmlelements for following keys
* : @"attributedString" // html main body
* : @"insets" // images and/or videos with range info
* : @"as" // href with range info
*
*/
- (NSMutableDictionary*) toHTMLElements;
NSString + HTML.m
- (NSMutableDictionary*) toHTMLElements {
// …
// handle escape encoding here
// assume that NSString* htmlString is the processed string;
// …
NSMutableDictionary * htmlElements = [[NSMutableDictionary dictionary] retain];
NSMutableAttributedString * attributedString = [[[NSMutableAttributedString alloc] init] autorelease];
NSMutableArray * insets = [NSMutableArray array];
NSMutableArray * as = [NSMutableArray array];
[htmlElements setObject:attributedString forKey:HTML_ATTRIBUTEDSTRING];
[htmlElements setObject:insets forKey:HTML_INSETS];
[htmlElements setObject:as forKey:HTML_AS];
// parse the HTML with an XML parser
// CXXML is a variance of TBXML (http://www.tbxml.co.uk/ ) which can handle the inline tags such as <span>
// code not available to public yet, so write your own inline-tag-enabled HTML/XML parser.
CXXML * xml = [CXXML tbxmlWithXMLString:htmlString];
TBXMLElement * root = xml.rootXMLElement;
TBXMLElement * next = root->firstChild;
while (next != nil) {
//
// do something here for special treatments if needed
//
NSString * tagName = [CXXML elementName:next];
[self appendXMLElement:next withAttributes:[HTMLElementAttributes defaultAttributesFor:tagName] toHTMLElements:htmlElements];
next = next->nextSibling;
}
return [htmlElements autorelease];
}
- (void) appendXMLElement:(TBXMLElement*)aElement withAttributes:(NSDictionary*)parentAttributes toHTMLElements:(NSMutableDictionary*) htmlElements {
// do your parse of aElement and its attribute values,
// assume NSString * tagAttrString is the parsed html attribute string (either from "style" attribute or css file) for this tag like : width:200px; color:#123456;
// let an external HTMLElementAttributes class to handle the attribute updates from the parent node's attributes
NSDictionary * tagAttr = [HTMLElementAttributes updateAttributes: parentAttributes withCSSAttributes:tagAttrString];
// create your NSAttributedString styled by tagAttr
// create insets such as images / videos or hyper links objects
// then update the htmlElements for storage
// once this tag is handled, recursively visit and process the current tag's children
TBXMLElement * nextChild = aElement->firstChild;
while (nextChild != nil) {
[self appendXMLElement:nextChild withAttributes:tagAttr toHTMLElements:htmlElements];
nextChild = nextChild->nextSibling;
}
}