NSAttribute字符串转换为HTML [英] NSAttribute string to HTML
问题描述
我正在开发iOS中的应用程序,我的要求是将HTML字符串转换为NSAttributed字符串,并将NSAttributed字符串转换回HTML.第一部分我能够实现.但是没有将NSAttributed String转换为HTML.它以HTML形式返回字符串,但不是所期望的.请参见下面的代码和输出:
I am devloping an application in iOS, and my requirement is to covert HTML string to NSAttributed string and NSAttributed string back to HTML. First part I was able to achieve. But NSAttributed String to HTML is not happening. It returns the string in HTML but not as desired. Please see code and output below:
HTML到NSAttributed字符串
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];
上面的代码按需工作....
The above code works as desired....
将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];
此代码不起作用..而且我没有得到想要的输出.我得到的输出是:
This code does not work.. and i do not get desired output.. the output i get is:
<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>
推荐答案
在引用了一些链接后,我找到了该解决方案,并使用了我的App,它将可以完美地工作.将您的HTML加载到虚拟Web视图中并获取正文html.您将获得原始的html数据.
After refering some Links i Find this solution and i used my App it will perfectly work. Load your HTML into dummy web view and get body html. you get your original html Data.
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:如果在 NSAttributedString
中使用 NSFontAttributeName
,请从 NSFontAttributeName
./strong> *****感谢您从我的问题中学到的东西.*****
PS:If you use NSFontAttributeName
in NSAttributedString
Remove the NSFontAttributeName
from the NSAttributedString
. *****Thanks I learn something from your question.*****
这篇关于NSAttribute字符串转换为HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!