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

2019-01-05  本文已影响0人  一颗熊的自由时光

一.文本自适应

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 =@"<html><body> Some html string \n <font size=\"13\" color=\"red\">This is some text!</font> </body></html>";

 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;
上一篇 下一篇

猜你喜欢

热点阅读