4

I have NSTextView and I can have text as nsattributedstring. I can save text into .txt file using NSSavePanel, as plain text, but not as formatted text.

@IBAction func saveDNA(sender: AnyObject)
{
    let saveDNAtoFile:  NSSavePanel = NSSavePanel()
    saveDNAtoFile.canSelectHiddenExtension = true
    saveDNAtoFile.runModal()

    do
    {
        let exportedFileURL = saveDNAtoFile.URL
        let textDNA = self.inputDnaFromUser.string

        if exportedFileURL != nil
        {
            try textDNA!.writeToURL(exportedFileURL!, atomically: false, encoding: NSUTF8StringEncoding)
        }
    } catch
    {
    }
}

How can I save the attributedstring (text) into file using NSSavePanel, to be able later to open this file to have all made before formatting in the text? What I should change in the code above, if I can use NSSavePanel for this ?

VYT
  • 1,071
  • 19
  • 35

2 Answers2

2

AppKit and UIKit add many methods to NSAttributedString for serializing and deserializing. Formerly they were documented separately, but now they are part of the unified NSAttributedString documentation.

There are too many methods to list here, but in the documentation you will find methods to convert NSAttributedString to/from several formats including Rich Text Format (RTF), HTML (starting in macOS 10.15 and iOS 13), Markdown (starting in macOS 12 and iOS 15), and others. You can also convert to/from Data, in which case you specify the format by setting the appropriate documentType in the documentAttributes dictionary. The conversions to/from Data support a few formats for which there are no dedicated methods.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • 1
    The text view's `textStorage` property contains an `NSTextStorage`, and `NSTextStorage` is a subclass of `NSMutableAttributedString`. – rob mayoff Oct 17 '15 at 12:48
  • As beginner I have thought that such standard functionality as open and save standard text files should have standard piece of code everywhere in books and google. But it is not. I have two books "Swift development with Cocoa" and Cocoa programming for OSX. There is no such clear code examples. Why ? It is strange. – VYT Oct 17 '15 at 14:46
  • You could take a look at the [TextEdit sample code](https://developer.apple.com/library/mac/samplecode/TextEdit/Introduction/Intro.html). Use the download link to get a whole Xcode project. – rob mayoff Oct 17 '15 at 15:26
  • Thanks. But all this is in Objective-C, not swift. I have started to learn swift from zero two months ago, I don't know Objective-C. – VYT Oct 17 '15 at 15:56
  • Most Cocoa sample code is still written in Objective-C. You will have to become comfortable reading it. – rob mayoff Oct 17 '15 at 21:20
  • Broken link to "NSAttributedString AppKit Additions Reference" :( – Grimxn Aug 07 '17 at 10:44
2

One day out ... Ok, I have figured out the code for Swift 2 (note this - options: NSFileWrapperWritingOptions.Atomic). Below. I am sure it will save time for beginners like me, more time to write necessary and more interesting algorithms, than this standard functionality.

@IBAction func saveDNA(sender: AnyObject)
{
    let saveDNAtoFile:  NSSavePanel = NSSavePanel()
    saveDNAtoFile.canSelectHiddenExtension = true
    saveDNAtoFile.runModal()

    do
    {
        let exportedFileURL = saveDNAtoFile.URL

        let textDNA = inputDnaFromUser.textStorage

        if exportedFileURL != nil
        {
            let range = NSRange(0..<textDNA!.length)

            let textTSave = try textDNA!.fileWrapperFromRange(range, documentAttributes: [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType])
            try textTSave.writeToURL(exportedFileURL!, options: NSFileWrapperWritingOptions.Atomic, originalContentsURL: nil)

        }
    } catch
    {
    }
}
VYT
  • 1,071
  • 19
  • 35