美文网首页
使用NSAttributedString在UILabel上加在H

使用NSAttributedString在UILabel上加在H

作者: DeadRoach | 来源:发表于2016-07-12 15:02 被阅读173次

获取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上加在H

      本文链接:https://www.haomeiwen.com/subject/mstsjttx.html