2014-02-24 2 views
1

<h2>..</h2>, <p>..</p><a href=".."><img ..></a> 요소로 구성된 HTML 데이터를 적절한 형식으로 attributedString으로 변환해야합니다. <h2>UIFontTextStyleHeadline1<p> ~ UIFontTextStyleBody을 할당하고 이미지 링크를 저장하고 싶습니다. 머리글 및 본문 요소 만 attributedString으로 출력해야하며 이미지를 개별적으로 처리해야합니다.HTML을 올바른 형식의 attributedString으로 변환

지금까지, 나는이 코드를 가지고 :

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의 새로운 오전과에 이러한 특성을 변환 할 수있는 효율적인 방법을 모색 : 이런 식으로 뭔가에 출력

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

표준 폰트. 고맙습니다.

답변

0

사람이 같은 AttributedString의의 서식 특성을 나는 HTML 데이터에 텍스트 요소에서 이미지를 분리하는 TFHpple의 librabry를 사용하여 끝이고 나는 변화 비슷한을 추구 할 경우

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 태그를 보유하고 단락 스타일에 깊이 파고 수있는 몇 가지 문제를 야기하는 경우 속성에서 글꼴 크기, 깨지기 쉬운 것처럼 보일 수 확인.