IOS中对UILabel设置行间距或者字间距,根据内容计算高度

//        //设置label的行间距

        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:weakSelf.fj_footerLable.text];

        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

        [paragraphStylesetLineSpacing:autoScaleW(5)];

        [attributedStringaddAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [weakSelf.fj_footerLable.text length])];

        [weakSelf.fj_footerLablesetAttributedText:attributedString];

//设置字间距

-(void)setLabelSpace:(UILabel)label withValue:(NSString)str withFont:(UIFont)font {

NSMutableParagraphStyle

paraStyle = [[NSMutableParagraphStyle alloc] init];

paraStyle.lineBreakMode = NSLineBreakByCharWrapping;

paraStyle.alignment = NSTextAlignmentLeft;

paraStyle.lineSpacing = 6; //设置行间距

paraStyle.hyphenationFactor = 1.0;

paraStyle.firstLineHeadIndent = 0.0;

paraStyle.paragraphSpacingBefore = 0.0;

paraStyle.headIndent = 0;

paraStyle.tailIndent = 0;

//设置字间距 NSKernAttributeName:@1.5f

NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f

};

CGSize size = [str boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;

    return size.width;

}

//计算UILabel的高度(带有行间距的情况)

-(CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(UIFont*)font withWidth:(CGFloat)width

{

    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];

    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;

    paraStyle.alignment = NSTextAlignmentLeft;

    paraStyle.lineSpacing=autoScaleW(5);

    paraStyle.hyphenationFactor=1.0;

    paraStyle.firstLineHeadIndent = 0.0;

    paraStyle.paragraphSpacingBefore = 0.0;

    paraStyle.headIndent=0;

    paraStyle.tailIndent=0;

    NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f

                         };

    CGSize size = [str boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;

    returnsize.height;

}

你可能感兴趣的:(IOS中对UILabel设置行间距或者字间距,根据内容计算高度)