宽容他人,放过自己。

富文本AttributedString转换成html

Posted on By anchoriteFili

NSMutableAttributedString *AttrStr = [[NSMutableAttributedString alloc] initWithString:@"排名第 4 名"];
    //改变字体的颜色
    [AttrStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(3, AttrStr.length-3-1)]; //后边留一个字符不变
    //改变某个范围的字体的大小
    [AttrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(3, AttrStr.length-3-1)];
    
    NSLog(@"************ %@",[self htmlStringByHtmlAttributeString:AttrStr]);

引用:
textView富文本AttributedString,转换成html
求助IOS大牛 解决个富文本编辑问题!急急急?

- (NSString *)htmlStringByHtmlAttributeString:(NSAttributedString *)htmlAttributeString {
    
    NSString *htmlString;
    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;
}