Label文本自适应、富文本、加载html文字

一.文本自适应

self.bgview = [[UIView alloc]init];

UILable * label = [[UILabel alloc]init];

  [self.bgview addSubview:label];

  label.numberOfLines = 0;

  label.text= @"此处是文字";

  label.lineBreakMode = NSLineBreakByTruncatingTail;

//labelsize的高度的最小值,看到博客上有一些帖子设置的是最大值,用了一下,但是在我这里效果是不对的

   CGSize  LabelSize =CGSizeMake(UISCREENWIDTH-30,0);

   CGSize  expectSize = [label  sizeThatFits:LabelSize];

   label.frame=CGRectMake(15,0,expectSize.width, expectSize.height);

   self.bgview.frame = CGRectMake(0, 0, UISCREENWIDTH,CGRectGetMaxY(label.frame));

二.加载html文字

NSString* htmlString =@" Some html string \n This is some text! ";

 NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];

suggestLabel.attributedText= attrStr;

三.label富文本

NSMutableAttributedString *attrStr  = [[NSMutableAttributedString alloc]initWithString:self.suggest];

 NSRangerange1 = [[attrStrstring]rangeOfString:@"基本满足中国营养协会的建议值。"];

  [attrStraddAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:range1];

   suggestLabel.attributedText= attrStr;

你可能感兴趣的:(Label文本自适应、富文本、加载html文字)