html - 使用 NSHTMLTextDocumentType 属性在 UITextView 中使用无序列表重复项目符号

标签 html ios swift uitextview nsattributedstring

我正在 UITextView 中显示一些 HTML 内容。内容有要点。这些项目符号是使用无序列表 ( <ul> ) 创建的,但每个项目符号都是重复的。

enter image description here

我创建了 NSAttributedString 喜欢

let contentHTML = ...

guard let data = contentHTML.data(using: .utf8) else {
    return
}

let attributedOptions: [String: AnyObject] = [
    NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType as AnyObject,
    NSCharacterEncodingDocumentAttribute: NSNumber(value: String.Encoding.utf8.rawValue)
]

textView.attributedText = try! NSAttributedString(data: data, options: attributedOptions, documentAttributes: nil)

为什么项目符号点重复? 有什么想法吗?

谢谢

更新

HTML 内容类似于

<p>Let’s take a quick look.</p><br/><ul><br/><li>lksdfjalsdjlajf</li><br/><li>lasdkjflaksjdf</li><br/><li>...

最佳答案

所以,我做了一些测试,发现了导致问题的原因。基本上,\n 之间的每一个新行( <br><ul> )或<li>UITextView将创建一个额外的子弹。

一种解决方法可以是替换 <br/>空字符串:

contentHTML = contentHTML.replacingOccurrences(of: "</li><br/><li>", with: "</li><li>")
contentHTML = contentHTML.replacingOccurrences(of: "<ul><br/><li>", with: "<ul><li>")
contentHTML = contentHTML.replacingOccurrences(of: "</li><br/></ul>", with: "</li></ul>")

结果:

enter image description here

关于html - 使用 NSHTMLTextDocumentType 属性在 UITextView 中使用无序列表重复项目符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49360774/

相关文章:

ios - 有没有办法在滑动单元格时从 tableViewCell 导航到另一个 View Controller ?

ios - 在 TableView 中填充数组字典的高效且干净的方法

swift - Uber SSO 登录错误 Swift 2.3

iphone - 为什么具有相同格式的文本在 iPhone 上呈现不同

ios - iOS 中的手电筒闪光灯

objective-c - 使用 Core Data 计算一个简单的总和

ios - 核心数据 - 如何初始化对象但不将其添加到持久存储?

html - 试图制作一个包含文本的 slider

javascript - Facebook 如何获取我本地文件的完整路径?

html - 垂直滚动条的底部在页面上不可见