gpt4 book ai didi

ios - 将 HTML 标记转换为字符串

转载 作者:行者123 更新时间:2023-11-28 06:12:27 25 4
gpt4 key购买 nike

我想将 HTML Tag 转换成 String,我使用 enum 找到 String 的内容长度>,根据长度我需要做一些操作。

我的模态类,

class PostViewModel {
var content: TextContent
enum TextContent {
case expanded(String)
case collapsed(String)

static func == (lhs: TextContent, rhs: TextContent) -> Bool {
switch lhs {
case .collapsed(let content):
if case collapsed(content) = rhs {
return true
}
return false
case .expanded(let content):
if case expanded(content) = rhs {
return true
}
return false
}
}
}
}

我在 cellForItem(at index: Int) 中调用这个函数,

func applyVerticalSizeConcernedRendering(fromViewModel viewModel: PostViewModel) {
switch viewModel.content {
case .collapsed(let content):
let str = content.html2String
print(str)
case .expanded(let content):
break
}
}

html2StringString扩展

extension String {
var html2AttributedString: NSMutableAttributedString? {
guard let data = data(using: String.Encoding.utf8) else { return nil }
let attrs: [String: Any] = [
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue
]
do {
let attrStr = try NSMutableAttributedString(data: data, options: attrs, documentAttributes: nil)
return attrStr
} catch let error as NSError {
print(error.localizedDescription)
return nil
}
}
var html2String: String {
return html2AttributedString?.string ?? ""
}
}

问题是在将 HTML 标签转换为 String 时崩溃了

这里崩溃了,

let attrStr = try NSMutableAttributedString(data: data, options: attrs, documentAttributes: nil)

并且String不为空

下面是HTML String的例子,

<strong>I want to test HTML Tags<br><\/strong>dsfhjdjf sjdfdj  djfjdfj djkf dfjdhf <strong>adjf<br>asks <\/strong>djfdkf<br><strong>dfdjk dkfjdk <\/strong>dfjik iai <strong>adsfhj<\/strong>

当我尝试使用硬编码值时,它工作正常,但只有当我从 enum 中获取 String 时它才会崩溃

And i don't know why the <code>String</code> is placed inside brackets. I hope, that's causing the crash

崩溃日志是,

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'

有人可以帮忙吗?

最佳答案

试试这个,不需要做你已经完成的整个过程:

extension String {
var htmlAttributedString: NSAttributedString? {
do {
return try NSAttributedString(data: Data(utf8), options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
print("error:", error)
return nil
}
}
var htmlString: String {
return htmlAttributedString?.string ?? ""

}

用法:

let html = "<strong>I want to test HTML Tags<br></strong>dsfhjdjf sjdfdj  djfjdfj djkf dfjdhf <strong>adjf<br>asks</strong>djfdkf<br><strong>dfdjk dkfjdk </strong>dfjik iai <strong>adsfhj</strong>"
let str = html.htmlString

所以您基本上只是在您的字符串上使用字符串扩展。这是我在我的项目中使用的

更新:
上面打印了以下内容:

enter image description here

Here是您可以尝试的示例项目。

关于ios - 将 HTML 标记转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46155800/

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