项目中需要实现:
如图,该产品详情页“购买须知”以下部分并不是一个H5页面,而是由后台加载出来的最多20张图片上下拼接而成。这里图片的拼接,我使用了一个强大的富文本控件TYAttributedLabel来实现。
实现代码:
1、导入主头文件
#import "TYAttributedLabel.h"
2、计算拼接图片的总高度
CGFloat footerHeight = 0;
for (NSInteger i = 1; i < 21; i ++) {
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@%@",model.resUrl,model.productInfoList[0][@"productBasicAttr"][@"desUrlList"][@"value"],[NSString stringWithFormat:@"/rights%ld.jpg",(long)i]]]];
if (data) {
UIImage *image = [UIImage imageWithData:data];
CGFloat imgWidth = (CGFloat)SCREEN_WIDTH;
CGFloat imgHeight = imgWidth * image.size.height/image.size.width;
footerHeight += imgHeight;
}
}
//创建tableView底部拼接图片
[self foundTableFooterImage:footerHeight model:model];
3、创建tableView底部拼接图片
/**
创建带有拼接图片的富文本
*/
- (void)foundTableFooterImage:(CGFloat)height model:(CommodityModel *)model
{
UIView *tableFooterView = [[UIView alloc] initWithFrame:FRAME(0, 0, SCREEN_WIDTH, height)];
self.mainTableView.tableFooterView = tableFooterView;
TYAttributedLabel *label = [[TYAttributedLabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, height)];
[tableFooterView addSubview:label];
//追加图片
for (NSInteger i = 1; i < 21; i ++) {
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@%@",model.resUrl,model.productInfoList[0][@"productBasicAttr"][@"desUrlList"][@"value"],[NSString stringWithFormat:@"/rights%ld.jpg",(long)i]]]];
if (data) {
UIImage *image = [UIImage imageWithData:data];
CGFloat imgWidth = (CGFloat)SCREEN_WIDTH;
CGFloat imgHeight = imgWidth * image.size.height/image.size.width;
TYImageStorage *imageStorage = [[TYImageStorage alloc]init];
imageStorage.image = image;
imageStorage.imageAlignment = TYImageAlignmentCenter;
imageStorage.size = CGSizeMake(SCREEN_WIDTH, imgHeight);
[label appendTextStorage:imageStorage];
}
}
}