UITextView不显示RTF文件的格式化文本

Jas*_*n O 1 rtf resourcebundle uitextview ios swift

我正在为iOS 9.1制作应用程序(使用Xcode 7.1和Swift 2.0),其中包含UITextView用于显示来自rtf资源文件的格式化文本.我viewDidLoad()在视图控制器的函数中调用以下函数来加载文本:

func loadTutorialText() {
    if let rtfPath = NSBundle.mainBundle().URLForResource("TutorialText", withExtension: "rtf") {
        do {
            let attributedStringWithRtf = try NSAttributedString(URL: rtfPath, options: [NSDocumentTypeDocumentAttribute : NSRTFTextDocumentType], documentAttributes: nil)
            self.textView.attributedText = attributedStringWithRtf
        } catch {
            print("Error loading text")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行应用程序时,rtf资源文件中的文本已加载,但所有内容都显示为纯文本.这是rtf我正在使用的测试文件的示例:

示例标题

文本文本正文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本正文本文本文本文本文本文本文本文本文本文本文本.

示例标题

文本文本正文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本正文本文本文本文本文本文本文本文本文本文本文本.

项目符号列表

  • 第1项
  • 第2项
  • 第3项

我需要设置一些属性UITextView来正确显示吗?

mat*_*att 5

而不是设置documentAttributesto nil,将它们读入变量:

var d : NSDictionary? = nil
let attributedStringWithRtf = try NSAttributedString(
    URL: rtfPath, 
    options: [NSDocumentTypeDocumentAttribute : NSRTFTextDocumentType], 
    documentAttributes: &d)
Run Code Online (Sandbox Code Playgroud)

编辑您的RTF文件在我的机器上正常工作:

在此输入图像描述

从字面上看,该应用程序中唯一的代码是:

class ViewController: UIViewController {
    @IBOutlet weak var tv: UITextView!
    override func viewDidLoad() {
        super.viewDidLoad()
        let rtfPath = NSBundle.mainBundle().URLForResource("TutorialText", withExtension: "rtf")!
        var d : NSDictionary? = nil
        let attributedStringWithRtf = try! NSAttributedString(
            URL: rtfPath,
            options: [NSDocumentTypeDocumentAttribute : NSRTFTextDocumentType],
            documentAttributes: &d)
        self.tv.attributedText = attributedStringWithRtf
    }
}
Run Code Online (Sandbox Code Playgroud)

我不得不建议,如果那不是你所看到的,那么你还有其他代码,你还没有告诉我们这些代码的出现和搞砸事情.