一、将超文本转化为富文本
- (NSAttributedString *)htmlAttributeStringByHtmlString:(NSString *)htmlString
{
NSAttributedString * attributeString;
NSData * htmlData = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary * importParams = @{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:[NSNumber numberWithInt:NSUTF8StringEncoding]};
NSError * error = nil;
attributeString = [[NSAttributedString alloc] initWithData:htmlData options:importParams documentAttributes:NULL error:&error];
return attributeString;
}
二、 将富文本转化为超文本格式
- (NSString *)htmlStringByHtmlAttributeString:(NSAttributedString *)htmlAttributeString
{
NSDictionary * exportParams = @{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:[NSNumber numberWithInt:NSUTF8StringEncoding]};
NSData * htmlData = [htmlAttributeString dataFromRange:NSMakeRange:(0,htmlAttributeString.length) documentAttributes:exportParams error:nil];
htmlString = [[NSString alloc] initwithData:htmlData encoding:NSUTF8StringEncoding];
return htmlString;
}