使用NSAttributedString在UILabel上加在HTML

获取NSAttributedString显示html后的高度

NSAttributedString用法

UILabel* label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 500)];    

label.font = [UIFont systemFontOfSize:20];   

 label.numberOfLines = 0;    

label.backgroundColor = [UIColor greenColor];    

[self.view addSubview:label];        

NSString *html = @"Wow!Now iOS can create NSAttributedString from HTMLs!";    NSDictionary*options=@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType};    

NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF8StringEncoding] options:options documentAttributes:nil error:nil];    

[attrString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor]             range:NSMakeRange(0, attrString.length)];   

 [attrString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14]                      range:NSMakeRange(0, attrString.length)];   

label.attributedText = attrString; 

CGRect rect = [attrString boundingRectWithSize:CGSizeMake(320, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];    

label.frame = CGRectMake(0,20, 320, rect.size.height);

你可能感兴趣的:(使用NSAttributedString在UILabel上加在HTML)