Edward_JS
Edward_JS

Reputation: 149

load rtf file into UITextView in Swift 2

Can somebody help me to load an rtf text into UITextView with Swift 2? The answers I've gotten are old and out of date. The text is instructions on how to play the game I'm writing in an app. So far, all I've been able to do is to copy and paste all the rtf text into the placeholder box. This works for iPhones in the simulator, but when trying it in the iPad simulator or iPhone 6 Plus there appears double vertical scroll bars when I do this. It looks messy.

I also now have a real plain text of the same file, so we can try that too.

Upvotes: 12

Views: 9423

Answers (4)

MikeG
MikeG

Reputation: 4044

Swift 3

        if let rtfPath = Bundle.main.url(forResource: "SomeTextFile", withExtension: "rtf") {
            do {
                let attributedStringWithRtf:NSAttributedString = try NSAttributedString(url: rtfPath, options: [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType], documentAttributes: nil)
                self.textView.attributedText = attributedStringWithRtf
            } catch let error {
                print("Got an error \(error)") 
            }
        }

Swift 4 & 5

    if let rtfPath = Bundle.main.url(forResource: "someRTFFile", withExtension: "rtf") {
        do {
            let attributedStringWithRtf: NSAttributedString = try NSAttributedString(url: rtfPath, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.rtf], documentAttributes: nil)
            self.textView.attributedText = attributedStringWithRtf
        } catch let error {
            print("Got an error \(error)")
        }
    }

Upvotes: 40

Chucky
Chucky

Reputation: 408

Swift 3 Code:

if let rtfPath = Bundle.main.url(forResource: "description_ar", withExtension: "rtf") {
    do {
        let attributedStringWithRtf:NSAttributedString = try NSAttributedString(url: rtfPath, options: [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType], documentAttributes: nil)
            self.textView.attributedText = attributedStringWithRtf
        } catch {
            print("We got an error \(error)") //or handle how you want
        }
}

Edits and updates inspired by @MikeG
October 21 update inspired by @RAJAMOHAN-S and @biomiker

Upvotes: 1

Vignesh Kumar
Vignesh Kumar

Reputation: 598

You can read rtf file use following code in Swift 2.

Load RTF file

    let path = NSBundle.mainBundle().pathForResource("sample-rtf", ofType: "rtf")

    let contents: NSString
    do {
        contents = try NSString(contentsOfFile: path!, encoding: NSUTF8StringEncoding)
    } catch _ {
        contents = ""
    }

    let array : NSArray = contents.componentsSeparatedByString("\n");

    self.textView.text  = (array.objectAtIndex(0) as! String);

    //self.textView.text  = contents as String

Try using a plain text (txt) file instead of rtf. RTF files contain formatting information about the text as well. Thats the unnecessary stuff that you see after reading the content.

Open the rtf file in Mac TextEdit and press Cmd+Shift+T (this will convert it to plain text and remove all formatting) then save as a txt.

Load Text file

    let path = NSBundle.mainBundle().pathForResource("sample-text", ofType: "txt")

    let contents: NSString
    do {
        contents = try NSString(contentsOfFile: path!, encoding: NSUTF8StringEncoding)
    } catch _ {
        contents = ""
    }

     self.textView.text  = contents as String

Upvotes: 1

Charmi Gheewala
Charmi Gheewala

Reputation: 828

if let rtfPath = NSBundle.mainBundle().URLForResource("description_ar", withExtension: "rtf") 
{
    let attributedStringWithRtf = NSAttributedString(fileURL: rtfPath, options: [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType], documentAttributes: nil, error: nil)
    self.textView.attributedText = attributedStringWithRtf
}

Upvotes: 1

Related Questions