【问题标题】:NSAttribute string to HTMLNSAttributedstring 到 HTML
【发布时间】:2014-10-17 14:08:48
【问题描述】:

我正在 iOS 中开发一个应用程序,我的要求是将 HTML 字符串转换为 NSAttributed 字符串,并将 NSAttributed 字符串转换回 HTML。 我能够实现的第一部分。但是 NSAttributed String 到 HTML 并没有发生。它以 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];

此代码不起作用..我没有得到想要的输出.. 我得到的输出是:

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

【问题讨论】:

    标签: html string nsattributedstring


    【解决方案1】:

    在参考了一些链接后,我找到了这个解决方案,并使用了我的应用程序,它会完美运行。将您的 HTML 加载到虚拟 Web 视图中并获取正文 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:如果您在NSAttributedString 中使用NSFontAttributeName,请从NSAttributedString 中删除NSFontAttributeName *****谢谢我从您的问题中学到了一些东西。**** *

    【讨论】:

    • 这是一个 WebView 对象。
    猜你喜欢
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多