Jor*_*ray 3 html html-table uilabel ios swift
In my app, i want to display a text in a UILabel. I use HTML to store the text in my data base to dynamically from my text in my app. I actually use this (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
}
Run Code Online (Sandbox Code Playgroud)
It's work great for the HTML stuff i used like
<b>Text</b>
<i>Test</i>
And more...
Run Code Online (Sandbox Code Playgroud)
Now, i want to display a table in my label. This is the HTML code for the table :
<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>
Run Code Online (Sandbox Code Playgroud)
此代码显示表格形式,但表格中没有边框。我想像卷轴HTML渲染一样显示表格边框。有可能吗?
小智 5
奇怪的问题,我不明白为什么这个简单的方法不起作用,但是我设法通过向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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1025 次 |
| 最近记录: |