不是“简单”的方式,而是如何使用以下方法遍历字符串的属性:
- (void)enumerateAttributesInRange:(NSRange)enumerationRange
options:(NSAttributedStringEnumerationOptions)opts
usingBlock:(void (^)(NSDictionary *attrs, NSRange range, BOOL *stop))block
有一个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)
看起来它会做你想做的事。