如何将NSAttributedString转换为HTML字符串?
正如标题所说,现在我可以用
initWithHTML:documentAttributes:
简单地将HTML转换为NSAttributedString
,但我想在这里做的是反向。
是否有任何第三方库来实现这一目标?
@implementation NSAttributedString(HTML)
-(NSString *)htmlForAttributedString{
NSArray * exclude = [NSArray arrayWithObjects:@"doctype",
@"html",
@"head",
@"body",
@"xml",
nil
];
NSDictionary * htmlAtt = [NSDictionary
dictionaryWithObjectsAndKeys:NSHTMLTextDocumentType,
NSDocumentTypeDocumentAttribute,
exclude,
NSExcludedElementsDocumentAttribute,
nil
];
NSError * error;
NSData * htmlData = [self dataFromRange:NSMakeRange(0, [self length])
documentAttributes:htmlAtt error:&error
];
//NSAttributedString * htmlString = [[NSAttributedString alloc]initWithHTML:htmlData documentAttributes:&htmlAtt];
NSString * htmlString = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
return htmlString;
}
@end
没有找到相关结果
已邀请:
2 个回复
浩挎
将文档类型属性(
)设置为HTML(
):
坍锭嘉韭蓝