【发布时间】:2011-03-14 11:54:18
【问题描述】:
正如标题所示,现在我可以简单地将 HTML 转换为 NSAttributedString 和 initWithHTML:documentAttributes: ,但我想要在这里做的是相反的。
是否有任何第三方库可以实现这一目标?
@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
【问题讨论】:
-
标题询问如何将HTML转换为NSAttributeString,而问题反之亦然。
-
您发布的代码到底有什么问题?它应该可以工作。
标签: html cocoa nsattributedstring