You should download your rtf data asynchronously and use NSAttributedString initialiser init(data: NSData, options: [String : AnyObject], documentAttributes dict: AutoreleasingUnsafeMutablePointer<NSDictionary?>) throws
to load your data when completed:
// your web link
let rtfLink = "http://www.aliectronics.com.au/thefournobletruths.rtf"
// make sure your link is valid NSURL using guard
guard let rtfURL = NSURL(string: rtfLink ) else { return }
// creata a data task for your url
NSURLSession.sharedSession().dataTaskWithURL(rtfURL) {
(data, response, error) in
// use guard to make sure you get a valid response from the server and your data it is not nil and you got no errors otherwise return
guard
let httpURLResponse = response as? NSHTTPURLResponse where httpURLResponse.statusCode == 200,
let data = data where error == nil
else { return }
// you need to use dispatch async to update the UI
dispatch_async(dispatch_get_main_queue(), { () -> Void in
// NSAttributedString data initialiser throws an error so you need to implement Swift2 Do Try Catch error handling
do {
let attributedString = try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute : NSRTFTextDocumentType], documentAttributes: nil)
textView.attributedText = attributedString
textView.editable = false
print("attributedString=====start")
print(attributedString)
print("attributedString=====end")
} catch let error as NSError {
print(error.localizedDescription)
}
})
}.resume()