Convert NSAttributedString into Data for storage

时间:2017-04-10 00:14:57

标签: ios swift nsdata nsattributedstring

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

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

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

2 个答案:

答案 0 :(得分:6)

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

update 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>
*/

答案 1 :(得分:1)

请为我工作后对其进行检查。 您还可以使用以下功能来更新字体样式和字体。

Starting HotswapAgent '/usr/local/openjdk-11/lib/hotswap/hotswap-agent.jar'
HOTSWAP AGENT: 10:18:24.771 INFO (org.hotswap.agent.HotswapAgent) - Loading Hotswap agent {1.4.0} - unlimited runtime class redefinition.
HOTSWAP AGENT: 10:18:24.992 INFO (org.hotswap.agent.config.PluginRegistry) - Discovered plugins: [JdkPlugin, Hotswapper, WatchResources, ClassInitPlugin, AnonymousClassPatch, Hibernate, Hibernate3JPA, Hibernate3, Spring, Jersey1, Jersey2, Jetty, Tomcat, ZK, Logback, Log4j2, MyFaces, Mojarra, Omnifaces, ELResolver, WildFlyELResolver, OsgiEquinox, Owb, Proxy, WebObjects, Weld, JBossModules, ResteasyRegistry, Deltaspike, GlassFish, Vaadin, Wicket, CxfJAXRS, FreeMarker, Undertow, MyBatis]
openjdk version "11.0.5" 2019-10-15
OpenJDK Runtime Environment 18.9 (build 11.0.5+10)
Dynamic Code Evolution 64-Bit Server VM 18.9 (build 11.0.5+5-202001261315, mixed mode)

}