我想将NSAttributedString转换为html,如下所示:
This is a <i>string</i> with some <b>simple</b> <i><b>html</b></i> tags in it.
Run Code Online (Sandbox Code Playgroud)
不幸的是,如果你使用apple的内置系统,它会生成详细的基于CSS的html.(以下示例供参考)
那么如何从NSAttributedString生成简单的标记html?
我写了一篇非常详细,脆弱的电话来做这件事,这是一个糟糕的解决方案.
func simpleTagStyle(fromNSAttributedString att: NSAttributedString)->String {
// verbose, fragile solution
// essentially, iterate all the attribute ranges in the attString
// make a note of what style they are, bold italic etc
// (totally ignore any not of interest to us)
// then basically get the plain string, and munge it for those ranges.
// be careful with the annoying "multiple attribute" case
// (an alternative …Run Code Online (Sandbox Code Playgroud)