简体   繁体   English

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

[英]UITextView not showing formatted text from RTF File

I am making an app for iOS 9.1 (using Xcode 7.1 and Swift 2.0), which contains a UITextView for displaying formatted text from an rtf resource file. 我正在为iOS 9.1(使用Xcode 7.1和Swift 2.0)制作一个应用程序,其中包含一个UITextView用于显示rtf资源文件中的格式化文本。 I am calling the following function in the viewDidLoad() function of the view controller to load the text: 我在视图控制器的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")
        }
    }
}

When I run the app, the text from the rtf resource file is loaded but everything is displayed as plain text. 当我运行该应用程序时,将加载rtf资源文件中的文本,但所有内容均显示为纯文本。 Here's an example of the test rtf file I'm using: 这是我正在使用的测试rtf文件的示例:

Example Heading 标题示例

text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text. 文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本。

Example Heading 标题示例

text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text. 文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本。

Bulleted List 项目符号列表

  • Item 1 项目1
  • Item 2 项目2
  • Item 3 项目3

Are there some properties I need to set for the UITextView to display this properly? 我需要为UITextView设置一些属性以使其正确显示吗?

Instead of setting the documentAttributes to nil , read them into a variable: 与其将documentAttributes设置为nil ,不如将它们读入一个变量:

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

EDIT Your RTF file works fine on my machine: 编辑您的RTF文件在我的机器上可以正常工作:

在此处输入图片说明

Literally the only code in that app is: 从字面上看,该应用程序中唯一的代码是:

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

I would have to suggest that if that's not what you're seeing, you have other code that you have not told us about that's coming along and messing things up. 我要建议的是,如果这不是您所看到的,那么您还有其他代码没有告诉我们这正在发生并弄乱了一切。

删除“接口”构建器中添加到Font属性中的另一个Size类的所有字体大小设置,原因将消除。

SWIFT 4 SWIFT 4

[NSDocumentTypeDocumentAttribute : NSRTFTextDocumentType] [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType]

don't work for me in swift 4 不要为我迅速工作4

here is my solution: 这是我的解决方案:

let attributedStringWithRtf:NSAttributedString = try NSAttributedString(
                    url: rtfPath,
                    options: [.documentType: NSAttributedString.DocumentType.rtf],
                    documentAttributes: nil
                )
                self.textView.attributedText = attributedStringWithRtf

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

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