最近产品提出一个需求:标题+富文本+定制cell的表格显示,富文本字体大小可更改,图片可点击。
如果使用WKWebView来做,改字体比较困难,于是就想到了DTCoreText。
这里只讲解富文本这一块:
1.继承DTAttributedTextCell,创建cell
@interface EssayRichContentCell ()
@property (nonatomic, strong) NSMutableAttributedString *attributedContent;//富文本内容
@end
//MARK: -改变字体,布局
- (void)changeFontSize:(FontSize)size
{
//默认使用中号字体大小
if (size>0) {
[Config currentConfig].fontSize = @(size);
}
UIFont *currFont = [UIFont systemFontOfSize:[Config currentConfig].fontSize.floatValue];
//只修改内容字体大小
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedContent];
[text addAttributes:[self settingAttributesWithLineSpacing:3 FirstLineHeadIndent:2*size Font:currFont TextColor:[UIColor h1Color]] range:text.rangeOfAll];
//如果是图片,移除属性约束
[self.attributedContent enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0, self.attributedContent.length) options:0 usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
if ([value isKindOfClass:[DTImageTextAttachment class]]) {
[text removeAttribute:NSParagraphStyleAttributeName range:range];
}
}];
self.attributedTextContextView.attributedString = text;
}
2.在控制器中创建cell
- (EssayRichContentCell *)tableView:(UITableView *)tableView preparedCellForIndexPath:(NSIndexPath *)indexPath
{
//NSCache缓存cell
EssayRichContentCell *cell = [self.DTTextCellCache objectForKey:kEssayAttributedTextCellId];
if (!cell) {
cell = [tableView dequeueReusableCellWithIdentifier:kEssayAttributedTextCellId];
cell.attributedTextContextView.delegate = self;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell layoutViewWithEssayContent:self.dataArray[indexPath.section][indexPath.row]];
[self.DTTextCellCache setObject:cell forKey:kEssayAttributedTextCellId];
} else {
[cell reloadContent];
}
return cell;
}
//tableview代理
//高度计算
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat cellHeight = 0.0f;
if (indexPath.section == 0) {
//...
}
else if (indexPath.section == 1) {
switch (indexPath.row) {
case 0:
//富文本
{
EssayRichContentCell *cell = [self tableView:tableView preparedCellForIndexPath:indexPath];
cellHeight = [cell requiredRowHeightInTableView:tableView];
}
break;
//...
}
}
//...
return cellHeight;
}
- 接下来就是图片加载的重点了。
#pragma mark - DTAttributedTextContentViewDelegate
- (UIView *)attributedTextContentView:(DTAttributedTextContentView *)attributedTextContentView viewForAttachment:(DTTextAttachment *)attachment frame:(CGRect)frame{
//如果是图片,获取图片并显示,这里第一次得到的frame的宽高都为0,需要通过imageView代理计算,重新加载
if([attachment isKindOfClass:[DTImageTextAttachment class]])
{
DTLazyImageView *imageView = [[DTLazyImageView alloc] initWithFrame:frame];
// url for deferred loading
imageView.url = attachment.contentURL;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage:)];
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:tap];
imageView.delegate = self;
return imageView;
}
return nil;
}
#pragma mark - DTLazyImageViewDelegate
//MARK: - 根据获取图片size重新加载
- (void)lazyImageView:(DTLazyImageView *)lazyImageView didChangeImageSize:(CGSize)size
{
CGFloat width = kScreenWidth - 10.5 - 13;//自己设定的宽
CGSize imageSize = size;
// resize image size if needed
if (size.width > width) {
CGFloat height = width * (size.height/size.width);
imageSize = CGSizeMake(width, height);
}
NSURL *url = lazyImageView.url;
NSPredicate *pred = [NSPredicate predicateWithFormat:@"contentURL == %@", url];
BOOL didUpdate = NO;
// update all attachments that match this URL (possibly multiple images with same size)
EssayRichContentCell *cell = [self.DTTextCellCache objectForKey:kEssayAttributedTextCellId];
for (DTTextAttachment *oneAttachment in [cell.attributedTextContextView.layoutFrame textAttachmentsWithPredicate:pred]) {
// update attachments that have no original size, that also sets the display size
if (CGSizeEqualToSize(oneAttachment.displaySize, CGSizeZero)) {
oneAttachment.displaySize = imageSize;
didUpdate = YES;
}
}
if (didUpdate) {
//获取到图片的宽高,重新加载
[self.tableView reloadData];
}
}