美文网首页
html和NSAttributedString相互转换

html和NSAttributedString相互转换

作者: l富文本l | 来源:发表于2017-01-12 15:56 被阅读0次
- (NSString *)getHTMLWithAttributedString:(NSAttributedString *)attributedString {
    NSDictionary *exportParams = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
    NSData *htmlData = [attributedString dataFromRange:NSMakeRange(0, attributedString.length) documentAttributes:exportParams error:nil];
    NSString *htmlString = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"pt;" withString:@"px;"];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"pt}" withString:@"px}"];
    return htmlString;
}

- (NSAttributedString *)getAttributedStringWithHTML:(NSString *)htmlString{
    NSData *htmltest = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *importParams = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
    NSAttributedString *attString = [[NSAttributedString alloc] initWithData:htmltest options:importParams documentAttributes:nil error:nil];
    return attString;
}

相关文章

网友评论

      本文标题:html和NSAttributedString相互转换

      本文链接:https://www.haomeiwen.com/subject/daodbttx.html