NSAttribute string to HTML

笑着哭i 提交于 2019-12-21 20:45:48

问题


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 to NSAttributed String

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

The above code works as desired....

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

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>

回答1:


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:If you use NSFontAttributeName in NSAttributedString Remove the NSFontAttributeName from the NSAttributedString. *****Thanks I learn something from your question.*****



来源:https://stackoverflow.com/questions/21374145/nsattribute-string-to-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!