如果我没有 nstring 直接显示它,如何在 UITextView 中显示 HTML 文件?

How to display an HTML file in UITextView if I don't have nstring to display it directly?

有谁知道如何在 UITextView 中正确显示 HTML 文件?目前我可以在视图中显示文件,但我无法对 HTML 进行编码,以便标签不会出现在视图中。下面是我用来调用 UITextView

中的文件的代码
if let filepath = Bundle.main.path(forResource: "test", ofType: "html") {  
            do {  
                let contents = try String(contentsOfFile: filepath)  
                print(contents)  
                TextView.text = contents  

            } catch {  
                /  
            }  
        } else {  
            /  
        }  

从字符串中获取数据并使用init(data:options:documentAttributes:) 生成属性字符串的方法。

if let filepath = Bundle.main.path(forResource: "test", ofType: "html") {
    if let htmlString = try? String(contentsOfFile: filepath, encoding: String.Encoding.utf8), let data = htmlString.data(using: String.Encoding.unicode) {
        let attString = try? NSAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.html], documentAttributes: nil)
        textView.attributedText = attString
    }
}