0

How can I load an RTF file in a UITextView?

lujop
  • 13,504
  • 9
  • 62
  • 95

2 Answers2

3

Assuming you include a file in your app called rtfdoc.rtf this will get you started:

let rtf = NSBundle.mainBundle().URLForResource("rtfdoc", withExtension: "rtf", subdirectory: nil, localization: nil)

let attributedString = NSAttributedString(fileURL: rtf, options: [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType], documentAttributes: nil, error: nil)

let textView = UITextView(frame: CGRectMake(20,20,self.view.frame.size.width-40,self.view.frame.size.height-40))

textView.attributedText = attributedString

self.view.addSubview(textView)

Edit: Storyboard Version

Full code as requested. This assumes you have wired up an outlet from the storyboard.

import UIKit
import Foundation

class ViewController: UIViewController {

    @IBOutlet var textView: UITextView!
    override func viewDidLoad() {
        super.viewDidLoad()
        if let rtf = NSBundle.mainBundle().URLForResource("rtfdoc", withExtension: "rtf", subdirectory: nil, localization: nil) {

            let attributedString = NSAttributedString(fileURL: rtf, options: [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType], documentAttributes: nil, error: nil)
            textView.attributedText = attributedString
            textView.editable = false
        }


    }

}

Note: text is not editable but it remains selectable.

sketchyTech
  • 5,746
  • 1
  • 33
  • 56
  • Thanks A lot, i was looking for this code many days. you are perfect. just one question, when i write this code it shows full page the RTF file text in my App. i want it to be half page. my UI TextView is half page with constraints. do you have any idea? i hope i explained well :) – Roberta Johnson Apr 17 '15 at 09:39
  • sorry how can i do in the way to be NON ediable ? – Roberta Johnson Apr 17 '15 at 10:13
  • You only need an outlet if you are connecting text view from storyboard etc. This is a programmatic approach. To make non-editable set editable property to false. – sketchyTech Apr 17 '15 at 10:59
  • Remove let textView line and self.view.addSubview() line. Replace textView with your outlet name – sketchyTech Apr 17 '15 at 11:30
  • can you write the sample code of Remove let textView line and self.view.addSubview() line. Replace textView with your outlet name – i tried but it gives Error. Really Thanks.. – Roberta Johnson Apr 17 '15 at 14:18
  • Congrats. I was just writing the code out before you added the comment, so thought I'd post it anyway. See above. – sketchyTech Apr 17 '15 at 14:30
  • @RobertaJohnson I'd be grateful if you could mark this as the answer if it has worked. Many thanks. – sketchyTech Apr 18 '15 at 10:40
  • Sure, can you tell how can i do that:) – Roberta Johnson Apr 20 '15 at 15:57
  • Click on the tick beneath the upvoter/downvoter on the left side of the answer. Might only appear when you hover (I've not asked a question for a while). – sketchyTech Apr 20 '15 at 16:01
3

with iOS9 the above NSAttributed String will give error. You can try the below code for the same:

do{
    let attrString =
    try NSMutableAttributedString(URL: rtfPath, options: [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType], documentAttributes: nil)
                textView.attributedText = attrString 
}catch{}
Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
Shweta
  • 41
  • 1