gpt4 book ai didi

swift - NSAttributedString 到 HTML 到 NSAttributedString 颜色退化

转载 作者:可可西里 更新时间:2023-10-31 23:46:07 27 4
gpt4 key购买 nike

我有一个富文本编辑器,用户可以在其中编辑字体、颜色等。模型将此信息作为 NSAttributedString 的实例保存在内存中。当我想存储此信息并写入磁盘时,我通过以下函数将属性字符串转换为 html:

func attributedStringToHtml(attributedString: NSAttributedString) -> String {
var ret = ""
do {
let htmlData = try attributedString.data(from: NSRange( location: 0, length: attributedString.length), documentAttributes: [.documentType: NSAttributedString.DocumentType.html])
ret = String.init(data: htmlData, encoding: String.Encoding.utf8)!
} catch { print("error:", error) }
return ret
}

并将数据从磁盘拉回到 NSAttributedString 的实例中,我使用:

func htmlToAttributedString(htmlString: String) -> NSAttributedString {
return NSAttributedString.init(html: htmlString.data(using: String.Encoding.utf8)!, documentAttributes: nil)!
}

多次循环这些函数会出现问题,即:

保存 -> 加载 -> 保存 -> 加载 -> 保存 -> 加载

每次加载循环后,存储的颜色会变得越来越深。我相信这与色彩空间的转换有关?

此行为不会发生在 macOS 10.13 上。将其复制并粘贴到 playground 中作为正在发生的事情的示例:

import AppKit
import PlaygroundSupport

func htmlToAttributedString(htmlString: String) -> NSAttributedString {
return NSAttributedString.init(html: htmlString.data(using: String.Encoding.utf8)!, documentAttributes: nil)!
}

func attributedStringToHtml(attributedString: NSAttributedString) -> String {
var ret = ""
do {
let htmlData = try attributedString.data(from: NSRange( location: 0, length: attributedString.length), documentAttributes: [.documentType: NSAttributedString.DocumentType.html])
ret = String.init(data: htmlData, encoding: String.Encoding.utf8)!
} catch {
print("error:", error)
}
return ret
}

let initialHtmlString = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n<meta http-equiv=\"Content-Style-Type\" content=\"text/css\">\n<title></title>\n<meta name=\"Generator\" content=\"Cocoa HTML Writer\">\n<meta name=\"CocoaVersion\" content=\"1671.5\">\n<style type=\"text/css\">\np.p1 {margin: 0.0px 0.0px 0.0px 0.0px; text-align: center; line-height: 16.0px; font: 14.0px Helvetica; color: #FF0000; -webkit-text-stroke: #000000}\nspan.s1 {font-kerning: none}\n</style>\n</head>\n<body>\n<p class=\"p1\"><span class=\"s1\">Double-click to edit this text</span></p>\n</body>\n</html>\n"

var attributedString = htmlToAttributedString(htmlString: initialHtmlString)
var backToHtml = attributedStringToHtml(attributedString: attributedString)
var backToAttributedString = htmlToAttributedString(htmlString: backToHtml)
var backToHtmlAgain = attributedStringToHtml(attributedString: backToAttributedString)

print(backToHtmlAgain) // notice the html color value is now f6000b

最佳答案

我能够通过使用 NSAttributedString.DocumentType.rtf 代替 html 来解决这个问题

关于swift - NSAttributedString 到 HTML 到 NSAttributedString 颜色退化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57102956/

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