富文本AttributedString转换成html

相关链接

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

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]);
- (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;
}

猜你喜欢

转载自blog.csdn.net/github_33467146/article/details/81207629
今日推荐