将属性文本转换为 HTML [英] Convert attributed text to HTML

查看:52
本文介绍了将属性文本转换为 HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的情况下,我必须将 NSHTMLTextDocumentType 文档转换为 NSMutableAttributedString,我使用下面的代码片段

In my case I have to convert NSHTMLTextDocumentType document to NSMutableAttributedString for which I use below snippet

[[NSMutableAttributedString alloc] initWithData:[text dataUsingEncoding:NSUTF8StringEncoding]
                                                   options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                             NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
                                        documentAttributes:nil error:nil];

当我登录我的 text 时,我会在下面看到附加的屏幕截图

When I log my text I get below for attached screenshot

<font color="#ff66b2">dasdasd asd asd asd sa asda&nbsp;</font>
<font color="#00feff">asdasdas &nbsp;asd </font>
<font color="#0000ff">sa asd asdasdsad</font> 

因为它是一个 UITextView 我需要在 iPhone 中也提供编辑支持.那么如何将 NSMutableAttributedString 转换为记录的字符串格式?

Since it's a UITextView I need to give edit support also in iPhone. So how can I convert NSMutableAttributedString into the formate of string logged?

任何帮助将不胜感激..

Any help would be greatly appreciated..

推荐答案

部分同意的答案

var htmlEncoding: String? {
do {

    let attrubutedString = NSAttributedString(string: self)

    let htmlData = try attrubutedString.data(from:NSMakeRange(0, attrubutedString.length), documentAttributes:[.documentType: NSAttributedString.DocumentType.html]);

    return String.init(data: htmlData, encoding: String.Encoding.utf8)

} catch {

    return nil

}

}

这篇关于将属性文本转换为 HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆