我在iOS中开发了一个应用程序,我的要求是将HTML字符串转换为NSAttributed字符串,并将NSAttributed字符串转换回HTML。 第一部分我能够实现。但NSAttributed String to HTML并没有发生。它以HTML格式返回字符串,但不是所希望的。 请参阅下面的代码和输出:
HTML到NSAttributed String
NSString *htmlString = @"<div>Test, </div><div> </div><div>Test Test Test</div><div>Test Test Test </div><div> </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> </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 String to 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];
此代码不起作用..我没有得到所需的输出.. 我得到的输出是:
<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 :(得分: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:如果您在NSFontAttributeName
中使用NSAttributedString
,请从NSFontAttributeName
移除NSAttributedString
。 *****谢谢我了解了一些事情从你的问题。*****