简体   繁体   English

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

[英]load rtf file into UITextView in Swift 2

Can somebody help me to load an rtf text into UITextView with Swift 2? 有人可以帮我用Swift 2将rtf文本加载到UITextView中吗? The answers I've gotten are old and out of date. 我得到的答案已经过时了。 The text is instructions on how to play the game I'm writing in an app. 该文本是关于如何玩我正在应用程序中编写的游戏的说明。 So far, all I've been able to do is to copy and paste all the rtf text into the placeholder box. 到目前为止,我所能做的就是将所有rtf文本复制并粘贴到占位符框中。 This works for iPhones in the simulator, but when trying it in the iPad simulator or iPhone 6 Plus there appears double vertical scroll bars when I do this. 这适用于模拟器中的iPhone,但是当我在iPad模拟器或iPhone 6 Plus中尝试时,出现这种情况时会出现双垂直滚动条。 It looks messy. 它看起来很乱。

I also now have a real plain text of the same file, so we can try that too. 我现在也有一个相同文件的真实纯文本,所以我们也可以尝试。

Swift 3 斯威夫特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)") 
            }
        }

Swift 4 & 5 斯威夫特4和5

    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)")
        }
    }
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
}

You can read rtf file use following code in Swift 2. 您可以在Swift 2中使用以下代码读取rtf文件。

Load RTF file 加载RTF文件

    let path = NSBundle.mainBundle().pathForResource("sample-rtf", ofType: "rtf")

    let contents: NSString
    do {
        contents = try NSString(contentsOfFile: path!, encoding: NSUTF8StringEncoding)
    } catch _ {
        contents = ""
    }

    let array : NSArray = contents.componentsSeparatedByString("\n");

    self.textView.text  = (array.objectAtIndex(0) as! String);

    //self.textView.text  = contents as String

Try using a plain text (txt) file instead of rtf. 尝试使用纯文本(txt)文件而不是rtf。 RTF files contain formatting information about the text as well. RTF文件也包含有关文本的格式信息。 Thats the unnecessary stuff that you see after reading the content. 这是你阅读内容后看到的不必要的东西。

Open the rtf file in Mac TextEdit and press Cmd+Shift+T (this will convert it to plain text and remove all formatting) then save as a txt. 在Mac TextEdit中打开rtf文件,然后按Cmd + Shift + T(这会将其转换为纯文本并删除所有格式),然后另存为txt。

Load Text file 加载文本文件

    let path = NSBundle.mainBundle().pathForResource("sample-text", ofType: "txt")

    let contents: NSString
    do {
        contents = try NSString(contentsOfFile: path!, encoding: NSUTF8StringEncoding)
    } catch _ {
        contents = ""
    }

     self.textView.text  = contents as String

Swift 3 Code: Swift 3代码:

if let rtfPath = Bundle.main.url(forResource: "description_ar", withExtension: "rtf") {
    do {
        let attributedStringWithRtf:NSAttributedString = try NSAttributedString(url: rtfPath, options: [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType], documentAttributes: nil)
            self.textView.attributedText = attributedStringWithRtf
        } catch {
            print("We got an error \(error)") //or handle how you want
        }
}

Edits and updates inspired by @MikeG 受@MikeG启发的编辑和更新
October 21 update inspired by @RAJAMOHAN-S and @biomiker 10月21日更新的灵感来自@ RAJAMOHAN-S和@biomiker

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM