2014-01-27 71 views
3

我在iOS中开发了一个应用程序,我的要求是将HTML字符串转换为NSAttributed字符串和NSAttributed字符串转换回HTML。 我能够实现的第一部分。但是NSAttributed String to HTML没有发生。它返回HTML中的字符串,但不是按照需要。 请参考下面的代码和输出:上述代码工作所需NSAttribute string to HTML

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]; 

....

NSAttributed字符串为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加载到虚拟web视图并获取body 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:如果您在使用NSAttributedStringNSFontAttributeNameNSAttributedString取出NSFontAttributeName *****谢谢我从你的问题中学到了一些东西。*****

+0

webEditor在你的最后一行代码中是什么? –

+0

这是一个WebView对象。 –