iOS笔记

yykit计算高度

2021-08-06  本文已影响0人  狗蛋的春天

1、model.h

    @property (nonatomic,strong) YYTextLayout *textLayout;
    @property (nonatomic,assign) CGFloat headViewHeight;

2、model.m

    - (YYTextLayout *)textLayout{
      CGSize size = CGSizeMake(kScreenWidth - 56, CGFLOAT_MAX);
      if (kStringIsEmpty(self.materialTitle)) {
          self.materialTitle = @"";
      }
      NSMutableAttributedString *attString = [[NSMutableAttributedString alloc]initWithString:self.materialTitle];
      NSRange range = NSMakeRange(0, self.materialTitle.length);

      [attString addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:15]} range:range];
      [attString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexStr:@"#353634"] range:range];
      NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
      [paragraphStyle setLineSpacing:5];
    [attString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];

    YYTextContainer *container = [YYTextContainer containerWithSize:size];
    container.maximumNumberOfRows = 2;//最多显示两行,如果0 则不限制行数
    container.truncationType = YYTextTruncationTypeEnd;
    container.size = size;
    YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:attString];
    return layout;
  }

3、cell里面

    contentlabel.textLayout = model.textLayout
    contentlabel.size_ = model.textLayout.textBoundingSize
    contentlabel.lineBreakMode = .byTruncatingTail
    contentlabel.snp.updateConstraints { (make) in
        make.height.equalTo(CGFloat((model.textLayout.textBoundingSize.height)))
    }
上一篇下一篇

猜你喜欢

热点阅读