使用NSAttributedString在UILabel上加在HTML

96
DeadRoach
2016.07.12 15:02* 字数 142

获取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);

iOS
Web note ad 1