html和NSAttributedString相互转换

2017-01-12  本文已影响0人  l富文本l
- (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;
}
上一篇 下一篇

猜你喜欢

热点阅读