1. 程式人生 > >Lable 富文字的插入圖片、設定行距、計算高度等

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;

    

}