将NSAttributedString转换为Data进行存储 [英] Convert NSAttributedString into Data for storage

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

问题描述

我有 UITextView ,带有属性文本, allowsEditingTextAttributes 设置为 true

I have a UITextView with attributed text and allowsEditingTextAttributes set to true.

我正在尝试使用以下代码将属性字符串转换为Data对象:

I'm trying to convert the attributed string into a Data object, using the following code:

let text = self.textView.attributedText
let data = try text.data(from: NSMakeRange(0, text.length), documentAttributes: [:])

然而,这会引发以下错误:

However, this is throwing the following error:

Error Domain=NSCocoaErrorDomain Code=66062 "(null)"

任何想法这个错误意味着什么或可能导致什么?我正在使用最新的Xcode和iOS。谢谢。

Any ideas what this error means or what could cause this? I'm on the latest Xcode and iOS. Thanks.

推荐答案

您需要指定要将属性字符串转换为哪种文档数据:

You need to specify what kind of document data you would like to convert your attributed string to:

NSPlainTextDocumentType   // Plain text document. .txt document
NSHTMLTextDocumentType    // Hypertext Markup Language .html document.
NSRTFTextDocumentType     // Rich text format document. .rtf document.
NSRTFDTextDocumentType    // Rich text format document with attachment. (.rtfd) document.






Xcode 8.3.2•Swift 3.1

let textView = UITextView()
textView.attributedText = NSAttributedString(string: "abc", attributes: [NSFontAttributeName: UIFont(name: "Helvetica", size: 16)])
if let attributedText = textView.attributedText {
    do {
        let htmlData = try attributedText.data(from: NSRange(location: 0, length: attributedText.length), documentAttributes: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType])
        let htmlString = String(data: htmlData, encoding: .utf8) ?? "" 
    } catch {
        print(error)
    }
}






更新Xcode 9.0.1•Swift 4

let textView = UITextView()
let attributes: [NSAttributedStringKey: Any] = [.font: UIFont(name: "Helvetica", size: 16)!]
textView.attributedText = NSAttributedString(string: "abc", attributes: attributes)
if let attributedText = textView.attributedText {
    let documentAttributes: [NSAttributedString.DocumentAttributeKey: Any] = [.documentType: NSAttributedString.DocumentType.html]
    do {
        let htmlData = try attributedText.data(from: NSRange(location: 0, length: attributedText.length), documentAttributes: documentAttributes)
        let htmlString = String(data: htmlData, encoding: .utf8) ?? ""
        print(htmlString)
    } catch {
        print(error)
    }
}






这将打印


This will print

/* <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<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: 16.0px Helvetica}
span.s1 {font-family: 'Helvetica'; font-weight: normal; font-style: normal; font-size: 16.00pt}
</style>
</head>
<body>
<p class="p1"><span class="s1">abc</span></p>
</body>
</html>
*/

这篇关于将NSAttributedString转换为Data进行存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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