【问题标题】:NSAttributedString to HTML to NSAttributedString color degradationNSAttributedString to HTML to NSAttributedString 颜色退化
【发布时间】:2019-07-19 08:58:15
【问题描述】:

我有一个富文本编辑器,用户可以在其中编辑字体、颜色等。模型将此信息作为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 上发生。 将其复制并粘贴到操场上,作为正在发生的事情的示例:

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

【问题讨论】:

标签: swift cocoa nsattributedstring nscolor


【解决方案1】:

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

【讨论】:

  • 您能指定更多信息吗?比如,你解释为 RTF 的有效载荷是什么样的? AKA initialHtmlString 的值是多少?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-03
  • 2013-04-28
  • 1970-01-01
  • 1970-01-01
  • 2021-01-13
  • 2013-12-17
  • 1970-01-01
相关资源
最近更新 更多