如何使用url而不是local来加载rtf

时间:2022-09-04 08:11:20

I have code to load local rtf files in textview but how to do i make it works for files located online

我有代码在textview中加载本地rtf文件,但如何使它适用于在线文件

Since it does not works when i use url Here is url - http://howtotechworld.com/rtfdoc.rtf

因为当我使用url时它不起作用这里是url - http://howtotechworld.com/rtfdoc.rtf

Here is code

这是代码

  if let rtf = NSBundle.mainBundle().URLForResource("http://howtotechworld.com/rtfdoc", withExtension: "rtf", subdirectory: nil, localization: nil) {
  do {
            let attributedString = try NSAttributedString(fileURL: rtf, options: [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType], documentAttributes: nil)
            textView.attributedText = attributedString
            textView.editable = false
            print(attributedString)
   }
   catch _ {
            NSLog("catched a error");
   }

2 个解决方案

#1


1  

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:

您应该异步下载rtf数据并使用NSAttributedString初始化init(数据:NSData,选项:[String:AnyObject],documentAttributes dict:AutoreleasingUnsafeMutablePointer )在完成时抛出加载数据: ?>

// 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()

#2


-1  

This works in a Playground (obviously needs better handling of optionals than forced unwrapping, but you get the point):

这适用于Playground(显然需要更好地处理选项而不是强制解包,但你明白了):

import UIKit

let url = NSURL(string: "http://thewalter.net/stef/software/rtfx/sample.rtf")
let data = NSData(contentsOfURL: url!)

do {
  let options = [NSDocumentTypeDocumentAttribute : NSRTFTextDocumentType]
  let attributedString = try NSAttributedString(data: data!, options: options, documentAttributes: nil)
  print(attributedString)
} catch {
  NSLog("\(error)")
}

#1


1  

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:

您应该异步下载rtf数据并使用NSAttributedString初始化init(数据:NSData,选项:[String:AnyObject],documentAttributes dict:AutoreleasingUnsafeMutablePointer )在完成时抛出加载数据: ?>

// 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()

#2


-1  

This works in a Playground (obviously needs better handling of optionals than forced unwrapping, but you get the point):

这适用于Playground(显然需要更好地处理选项而不是强制解包,但你明白了):

import UIKit

let url = NSURL(string: "http://thewalter.net/stef/software/rtfx/sample.rtf")
let data = NSData(contentsOfURL: url!)

do {
  let options = [NSDocumentTypeDocumentAttribute : NSRTFTextDocumentType]
  let attributedString = try NSAttributedString(data: data!, options: options, documentAttributes: nil)
  print(attributedString)
} catch {
  NSLog("\(error)")
}