计算label自定义行间距字间距,计算自定义行间距字间距的高度

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

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

    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;

    paraStyle.alignment = NSTextAlignmentLeft;

    paraStyle.lineSpacing = 4;

    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.0f

                          };

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

    return size.height;

}


CGFloat heightText = [self getSpaceLabelHeight:articleStr withFont:[UIFont systemFontOfSize:16.0] withWidth:OcToWidth - 10];

                

    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(5, boomHeight, OcToWidth - 10, heightText)];

    lable.font = [UIFont systemFontOfSize:16.0];

    lable.text = articleStr;

    NSMutableAttributedString *attributedString =  [[NSMutableAttributedString alloc] initWithString:lable.text attributes:@{NSKernAttributeName : @(1.0f)}];

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

    [paragraphStyle setLineSpacing:4];

    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, lable.text.length)];

    [lable setAttributedText:attributedString];

    lable.numberOfLines = 0;

     [self.mainScrollview addSubview:lable];



你可能感兴趣的:(计算自定义行间距字间距的高度,理论)