我需要将 HTML 转换回 HTML,而不是将其转换为属性字符串。这可以在 Mac 上轻松完成,如下所示:http://www.justria.com/2011/01/18/how-to-convert-nsattributedstring-to-html-markup/ http://www.justria.com/2011/01/18/how-to-convert-nsattributedstring-to-html-markup/
不幸的是,该方法dataFromRange:documentAttributes:
仅可在 Mac 上通过NSAttributedString
应用程序套件添加。
我的问题是如何在 iOS 上做到这一点?
这不是“简单”的方法,但是使用以下方法迭代字符串的属性怎么样:
- (void)enumerateAttributesInRange:(NSRange)enumerationRange
options:(NSAttributedStringEnumerationOptions)opts
usingBlock:(void (^)(NSDictionary *attrs, NSRange range, BOOL *stop))block
Have an NSMutableString
变量来累积 HTML(我们称之为“html”)。在该块中,您将使用字符串手动构建 HTML。例如,如果文本属性“attrs”指定红色粗体文本:
[html appendFormat:@"<span style='color:red; font-weight: bold;'>%@</span>", [originalStr substringWithRange:range]]
编辑:昨天偶然发现了这个:
“UliKit”中的 NSAttributedString+HTMLFromRange 类别
(https://github.com/uliwitness/UliKit/blob/master/NSAttributedString+HTMLFromRange.m https://github.com/uliwitness/UliKit/blob/master/NSAttributedString+HTMLFromRange.m)
看起来它会做你想做的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)