You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xcode 16中NSAttributedString渲染HTML有序列表异常求助

Xcode 16中NSAttributedString解析HTML有序列表丢失编号的问题

我正在把项目升级到Xcode 16,使用自定义的NSAttributedString扩展从HTML初始化富文本时遇到了问题:在Xcode 15中能正常渲染<ol><li>的有序列表(显示带编号的列表项),但Xcode 16里只显示列表项内容,完全看不到编号。

相关代码如下:

// 使用attributedText创建的标签
let label = UILabel()
label.attributedText = self.init(html: "<ol><li>1th item</li><li>2th item</li><li>3rd item</li></ol>")

// 用字符串而非数据初始化HTML格式NSAttributedString的便利方法
extension NSAttributedString {
    public convenience init(html: String) {
        guard let encodedData = html.data(using: String.Encoding.utf16) else {
            print("Unable to encode html string to data")
            self.init(string: html)
            return
        }
        assert(Thread.isMainThread, "The HTML importer should not be called from a background thread!")
        do {
            try self.init(
                data: encodedData,
                options: [
                    .documentType: NSAttributedString.DocumentType.html,
                    .characterEncoding: String.Encoding.utf16.rawValue,
                ],
                documentAttributes: nil
            )
        } catch {
            print("Initialising NSAttributedString with html failed \(error)")
            self.init()
        }
    }
}

预期效果是HTML解析后的富文本能正常显示有序列表的编号,实际效果却只有列表项文本,编号完全缺失。

想问下有没有其他开发者遇到过同样的问题?

内容的提问来源于stack exchange,提问作者user3297288

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 18:35:02