将rtf文件加载到Swift 2中的UITextView中

Edw*_*_JS 8 uitextview ios swift2

有人可以帮我用Swift 2将rtf文本加载到UITextView中吗?我得到的答案已经过时了.该文本是关于如何玩我正在应用程序中编写的游戏的说明.到目前为止,我所能做的就是将所有rtf文本复制并粘贴到占位符框中.这适用于模拟器中的iPhone,但是当我在iPad模拟器或iPhone 6 Plus中尝试时,出现这种情况时会出现双垂直滚动条.它看起来很乱.

我现在也有一个相同文件的真实纯文本,所以我们也可以尝试.

Mik*_*keG 28

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)") 
            }
        }
Run Code Online (Sandbox Code Playgroud)

Swift 4更新

    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)")
        }
    }
Run Code Online (Sandbox Code Playgroud)


小智 1

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
}
Run Code Online (Sandbox Code Playgroud)