gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 06:53:00 24 4
gpt4 key购买 nike

有谁知道如何在 UITextView 中正确显示 HTML 文件?目前我可以在 View 中显示文件,但我无法对 HTML 进行编码,以便标签不会出现在 View 中。下面是我用来在 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
}
}

关于ios - 如果我没有 nstring 直接显示它,如何在 UITextView 中显示 HTML 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55879726/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com