利用YYLabel实现高度计算和行间距设置,so 方便!

效果图奉上YYText 1.0.5

利用YYLabel实现高度计算和行间距设置,so 方便!_第1张图片
效果图

后台返回数据 "sdescription" : "当天制作,保质期7天\n提供免费包装\n免费包含蜡烛+刀叉+小盘\n仅限到店自提,不提供配送服务"

    //商品介绍
    NSString *introStr = @"";
    self.proIntroductLB.frame = CGRectMake(5, introductH+10, 80, 12);//【商品介绍】
    if (aproDetailModel.sdescription.length > 0) {
        introStr = aproDetailModel.sdescription;
    }
    CGFloat introHeight = [self getMessageHeight:introStr andLabel:self.proIntroductLB1];
    self.proIntroductLB1.frame = CGRectMake(85, introductH+8, ScreenWidth-100, introHeight);
/**
 *  获取lb的高度(默认字体13,行间距8,lb宽ScreenWidth-100)
 *  @param mess lb.text
 *  @param lb (YYLabel *)label
 *  @return lb的高度
 */
-(CGFloat)getMessageHeight:(NSString *)mess andLabel:(YYLabel *)lb
{
    NSMutableAttributedString *introText = [[NSMutableAttributedString alloc] initWithString:mess];
    introText.yy_font = FontSet(13);
    introText.yy_lineSpacing = 8;
    lb.attributedText = introText;
    CGSize introSize = CGSizeMake(ScreenWidth-100, CGFLOAT_MAX);
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:introText];
    lb.textLayout = layout;
    CGFloat introHeight = layout.textBoundingSize.height;
    return introHeight;
}

【附】boundingRectWithSize: 该方法只是取得字符串的size, 如果字符串中包含\n \r 这样的字符,也只会把它当成字符来计算。

今天第一次尝试使用,更多更详细的使用说明以及原理,接下来有空研究了再分享!!!

你可能感兴趣的:(利用YYLabel实现高度计算和行间距设置,so 方便!)