gpt4 book ai didi

html - NSHTMLTextDocumentType 是否支持 HTML 表格?

转载 作者:可可西里 更新时间:2023-11-01 00:34:21 24 4
gpt4 key购买 nike

在我的应用程序中,我想在 UILabel 中显示文本。我使用 HTML 将文本存储在我的数据库中,以动态地从我的应用程序中的文本中获取。我实际上使用这个(Swift 3.2,iOS 8+):

if let data = text.data(using: .utf8) {
let htmlString = try? NSMutableAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil)

self.textLabel.attributedText = htmlString

}

它非常适合我使用的 HTML 内容

<b>Text</b>
<i>Test</i>
And more...

现在,我想在我的标签中显示一个表格。这是表格的 HTML 代码:

<table border="2px solid black">
<tr><th>Symbole</th><th>Å</th><th>↓</th><th>■</th><th>╩</th><th>¬</th><th>▓</th><th>Ø</th><th>±</th><th> º </th><th>¶</th><th>░</th></tr>
<tr><td>Utilisation</td><td>1</td><td>11</td><td>11</td><td>5</td><td>1</td><td>4</td><td>12</td><td>4</td><td>1</td><td>5</td><td>1</td></tr>
</table>

此代码显示表格形式,但表格中没有边框。我想像卷轴 HTML 渲染一样显示表格边框。可能与否?

最佳答案

奇怪的问题,我不明白为什么这个简单的事情不起作用,但是我设法通过向 NSAttributedString 添加随机属性使边框出现,这让我相信这是一个 NSAttributedString 渲染错误。

这是我使用的函数(这是 Swift 4,但可以转换为更早的版本):

extension String {
func attributedString() -> NSAttributedString? {
guard let data = self.data(using: String.Encoding.utf8,
allowLossyConversion: false) else { return nil }
let options: [NSAttributedString.DocumentReadingOptionKey : Any] = [
NSAttributedString.DocumentReadingOptionKey.characterEncoding : String.Encoding.utf8.rawValue,
NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.html
]
let htmlString = try? NSMutableAttributedString(data: data, options: options, documentAttributes: nil)

// Removing this line makes the bug reappear
htmlString?.addAttribute(NSAttributedStringKey.backgroundColor, value: UIColor.clear, range: NSMakeRange(0, 1))

return htmlString
}
}

关于html - NSHTMLTextDocumentType 是否支持 HTML 表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46546994/

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