相关疑难解决方法(0)

如何将NSAttributedString转换为HTML字符串?

正如标题所说,现在我可以简单地将HTML转换NSAttributedStringinitWithHTML: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
Run Code Online (Sandbox Code Playgroud)

html cocoa nsattributedstring

26
推荐指数
2
解决办法
1万
查看次数

标签 统计

cocoa ×1

html ×1

nsattributedstring ×1