使用 Swift 4 和 Xcode 从包中的 RTF 文件中加载数据的文本视图 [英] Load Text View with data from RTF file in bundle using Swift 4 and Xcode

查看:82
本文介绍了使用 Swift 4 和 Xcode 从包中的 RTF 文件中加载数据的文本视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在阅读文本文件并将该信息加载到文本视图中.我有 100 多个项目,每个项目都有相当多的文本,因此这种方法效果很好.

I am currently reading text files and loading that information into a text view. I have over 100 items and quite a bit of text per item so this method works well.

我的问题是我想让一些文本加粗,但不能使用标准文本文件来做到这一点,所以我想知道如何修改我目前拥有的代码,以便它加载 RTF 文件的内容而不是文本文件,当然在文本视图中正确显示.

My problem is that I would like to make some text bold and can not do that using a standard text file so I am wondering how I can modify the code I currently have so that it loads the contents of a RTF file instead of the text file, and of course displays correctly in the text view.

let myString = String(indexNumber)
let detailFile = "detail" + myString
if let filepath = Bundle.main.path(forResource: detailFile, ofType: "txt") {
    do {
        let contents = try String(contentsOfFile: filepath)
        detailsTextView.text = contents
    } catch {
        detailsTextView.text = detailFile + " contents could not be loaded"
    }
} else {
    detailsTextView.text = detailFile + " detail file could not be found"
}

推荐答案

Example from my own test code(RTF文件名为test.rtf",self.tv是文本视图):

Example from my own test code (the RTF file is called "test.rtf", self.tv is the text view):

let url = Bundle.main.url(forResource: "test", withExtension: "rtf")!
let opts : [NSAttributedString.DocumentReadingOptionKey : Any] =
    [.documentType : NSAttributedString.DocumentType.rtf]
var d : NSDictionary? = nil
let s = try! NSAttributedString(url: url, options: opts, documentAttributes: &d)
self.tv.attributedText = s

这篇关于使用 Swift 4 和 Xcode 从包中的 RTF 文件中加载数据的文本视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆