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 ?

1
  • There is an AppKit/UIKit category of NSAttributedString to be able to write RTF Commented Oct 16, 2015 at 20:31

2 Answers 2

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
    {
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.

6 Comments

The text view's textStorage property contains an NSTextStorage, and NSTextStorage is a subclass of NSMutableAttributedString.
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.
You could take a look at the TextEdit sample code. Use the download link to get a whole Xcode project.
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.
Most Cocoa sample code is still written in Objective-C. You will have to become comfortable reading it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.