2014-02-24 96 views
1

我需要將包含<h2>..</h2>,<p>..</p><a href=".."><img ..></a>元素的HTML數據轉換爲格式正確的歸屬字符串。我想分配<h2>UIFontTextStyleHeadline1<p>UIFontTextStyleBody並存儲圖像鏈接。我只需要將輸出與標題和正文元素進行歸屬,我將分別處理圖像。將HTML轉換爲格式正確的歸檔字符串

到目前爲止,我有這樣的代碼:

NSMutableAttributedString *content = [[NSMutableAttributedString alloc] 
     initWithData:[[post objectForKey:@"content"] 
    dataUsingEncoding:NSUTF8StringEncoding] 
       options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, 
        NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} 
    documentAttributes:nil error:nil]; 

其輸出到這樣的事情:

Heading 
{ 
    NSColor = "UIDeviceRGBColorSpace 0 0 0 1"; 
    NSFont = "<UICTFont: 0xd47bc00> font-family: \"TimesNewRomanPS-BoldMT\"; font-weight: bold; font-style: normal; font-size: 18.00pt"; 
    NSKern = 0; 
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 14.94, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (null), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 2"; 
    NSStrokeColor = "UIDeviceRGBColorSpace 0 0 0 1"; 
    NSStrokeWidth = 0; 
}{ 
    NSAttachment = "<NSTextAttachment: 0xd486590>"; 
    NSColor = "UIDeviceRGBColorSpace 0 0 0.933333 1"; 
    NSFont = "<UICTFont: 0xd47cdb0> font-family: \"Times New Roman\"; font-weight: normal; font-style: normal; font-size: 12.00pt"; 
    NSKern = 0; 
    NSLink = "http://www.placeholder.com/image.jpg"; 
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 12, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (null), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0"; 
    NSStrokeColor = "UIDeviceRGBColorSpace 0 0 0.933333 1"; 
    NSStrokeWidth = 0; 
} 
Body text, body text, body text. Body text, body text, body text. 
{ 
    NSColor = "UIDeviceRGBColorSpace 0 0 0 1"; 
    NSFont = "<UICTFont: 0xd47cdb0> font-family: \"Times New Roman\"; font-weight: normal; font-style: normal; font-size: 12.00pt"; 
    NSKern = 0; 
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 12, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (null), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0"; 
    NSStrokeColor = "UIDeviceRGBColorSpace 0 0 0 1"; 
    NSStrokeWidth = 0; 
} 

我是新來attributedString,尋求一種有效的方式將這些屬性到轉換上面提到的標準字體。謝謝。

回答

0

如果有人將尋求類似的東西,我就完了使用TFHpple librabry在HTML中分離數據從文本元素的圖像,然後我改變attributedString的格式屬性如下:

NSString *contentString = [self parseHTMLdata:bodyString]; 

NSMutableAttributedString *content = [[NSMutableAttributedString alloc] initWithData:[contentString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil]; 

// prepare new format 
NSRange effectiveRange = NSMakeRange(0, 0); 

NSDictionary *attributes; 

while (NSMaxRange(effectiveRange) < [content length]) { 

attributes = [content attributesAtIndex:NSMaxRange(effectiveRange) effectiveRange:&effectiveRange]; 

    UIFont *font = [attributes objectForKey:@"NSFont"]; 

    if (font.pointSize == 18.0f) { 

     [content addAttribute:NSFontAttributeName value:self.headlineFont range:effectiveRange]; 

    } else { 

     [content addAttribute:NSFontAttributeName value:self.bodyFont range:effectiveRange]; 
    } 
} 

而且hpple部分:

- (NSString *)parseHTMLdata:(NSString *)content 
{ 
    NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding]; 

    TFHpple *parser = [[TFHpple alloc] initWithHTMLData:data]; 

    NSString *xpathQueryString = @"//body"; 

    NSArray *elements = [[[parser searchWithXPathQuery:xpathQueryString] firstObject] children]; 

    NSMutableString *textContent = [[NSMutableString alloc] init]; 

    for (TFHppleElement *element in elements) { 

     if ([[element tagName] isEqualToString:@"h2"] || [[element tagName] isEqualToString:@"p"]) { 

      if ([[[element firstChild] tagName] isEqualToString:@"a"]) { 

       // image element, just save it in array 
      } else { 

       // pure h2 or p element 
       [textContent appendString:[element raw]]; 
      } 
     } 
    } 

    return textContent; 
} 

檢查在屬性的字體大小可能看起來脆弱,如果它會引起一些問題,我可以更深入地保持航向/ body標籤段落樣式。

相關問題