宽容他人,放过自己。

解析html字符串获取属性字符串

Posted on By anchoriteFili

752372-20161111102907811-1605894355.png

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    NSString * htmlString =  @"<html><body> 事物联系人 <font color=\"red\">呵呵哒</font>的  <font color=\"red\">订单修改-修改价格</font> 事物 </body></html>";
    
    NSAttributedString *str = [self getStringWithParagraphStyleFromHtmlString:htmlString];
    self.textView.attributedText = str;
    
}


- (NSAttributedString *)getStringWithParagraphStyleFromHtmlString:(NSString *)string{
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithData:[string dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:3.5];
    [attrStr addAttribute:NSParagraphStyleAttributeName
                    value:paragraphStyle
                    range:NSMakeRange(0, attrStr.length)];
    
    return attrStr;
}