Lable 富文本的插入图片、设置行距、计算高度等

+ (NSMutableAttributedString *)MutableAttributedString:(NSString *)attri andatIndex:(NSInteger)Index  andImage:(UIImage *)attchImage andFrame:(CGRect)frame{

    

    NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc] initWithString:attri];

    

    //插入图片

    NSTextAttachment *attchString = [[NSTextAttachment alloc] init];

    attchString.image = attchImage;

    NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attchString];

    attchString.bounds = frame;

    [attriString insertAttributedString:string atIndex:Index];

 

   //设置行高,字间距,字体大小等属性

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

    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;

    paraStyle.alignment = NSTextAlignmentLeft;

    paraStyle.lineSpacing = 5;

    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:14.0f],NSParagraphStyleAttributeName:paraStyle,NSKernAttributeName:@1.5f };

    [attriString addAttributes:dic range:NSMakeRange(0, attri.length)];

    

    //获取整个富文本的size(高宽)

    CGSize size = [attriString boundingRectWithSize:CGSizeMake(200, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;

 

    

    NSLog(@"高度:%.2f",size.height);

    

    return attriString;

    

}

你可能感兴趣的:(UILable)