2014-01-27 2 views
3

iOS에서 응용 프로그램을 개발 중이며 HTML 문자열을 NSAttributed 문자열로 변환하고 NSAttributed 문자열을 HTML로 변환해야합니다. 첫 번째 부분은 달성 할 수있었습니다. 그러나 NSAttributed String to HTML은 일어나지 않습니다. 문자열을 HTML로 반환하지만 원하는 것은 아닙니다. 아래 코드와 출력을 참조하십시오NSAttribute 문자열을 HTML로

NSAttributed 문자열이 HTML로 .... 위의 코드는 원하는 작품으로 NSAttributed 문자열

NSString *htmlString = @"<div>Test, </div><div>&nbsp;</div><div>Test Test Test</div><div>Test Test Test </div><div>&nbsp;</div><div>Format text:</div><ul style="margin:0;padding-left:36pt;"><li><b>bold text</b></li><li><i>italic text</i></li><li><font color="red"><i>red text</i></font></li></ul><div>&nbsp;</div><div><i>The meeting body</i><i> </i><i>attachments</i></div>"; 

    NSAttributedString *normal = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil]; 




textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 20, 500, 500)]; 
    textView.attributedText = normal; 
    [self.view addSubview:textView]; 

HTML을

NSAttributedString *s = textView.attributedText; 
    NSDictionary *documentAttributes = [NSDictionary dictionaryWithObjectsAndKeys:NSHTMLTextDocumentType, NSDocumentTypeDocumentAttribute, nil]; 
    NSData *htmlData = [s dataFromRange:NSMakeRange(0, s.length) documentAttributes:documentAttributes error:NULL]; 
    NSString *htmlString = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding]; 

이 코드는 작동하지 않으며 .. 나는 욕망을 갖지 않습니다. D 출력은 .. 내가 얻을 출력은 :이 솔루션을 찾아 내가 그것을 완벽하게 작동합니다 내 응용 프로그램을 사용

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<meta http-equiv="Content-Style-Type" content="text/css"> 
<title></title> 
<meta name="Generator" content="Cocoa HTML Writer"> 
<style type="text/css"> 
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Times New Roman'; color: #000000; -webkit-text-stroke: #000000} 

span.s1 {font-family: 'Times New Roman'; font-weight: normal; font-style: normal; font-size: 12.00pt; font-kerning: none} 
</style> 
</head> 
<body> 
<p class="p1"><span class="s1">Test<span class="Apple-converted-space"> </span></span></p> 
<p class="p1"><span class="s1"> </span></p> 
<p class="p1"><span class="s1">Test Test<span class="Apple-converted-space"> </span></span></p> 
<p class="p1"><span class="s1"> </span></p> 
<p class="p1"><span class="s1"> </span></p> 
<p class="p1"><span class="s1"> </span></p> 
<p class="p1"><span class="s1"> </span></p> 
</body> 
</html> 

답변

0

일부 링크를 다스 려 후. 더미 웹보기로 HTML을로드하고 본문 HTML을 가져옵니다. 당신은 당신의 본래 html 자료를 얻는다.

NSAttributedString *s = textView.attributedText; 
    NSDictionary *documentAttributes = [NSDictionary dictionaryWithObjectsAndKeys:NSHTMLTextDocumentType, NSDocumentTypeDocumentAttribute, nil]; 
    NSData *htmlData = [s dataFromRange:NSMakeRange(0, s.length) documentAttributes:documentAttributes error:NULL]; 
    NSString *htmlString = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding]; 
    [webView loadHTMLString:htmlString baseURL:nil]; 
    NSString *BodyHTML = [webEditor stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"]; 

PS : 당신이 NSAttributedStringNSFontAttributeName를 사용하는 경우 NSAttributedString에서 NSFontAttributeName를 제거합니다. ***** 질문에 대한 답변을드립니다. *****

+0

마지막 코드 줄에는 어떤 웹 에디터가 있습니까? –

+0

WebView 개체입니다. –

관련 문제