DTCoreText主要用于富文本
倒入头文件
#import <DTCoreText/DTCoreText.h>
创建
@property (nonatomic, strong) DTAttributedLabel *contentLabel;
[_viewContent addSubview:({
_contentLabel = [DTAttributedLabel new];
_contentLabel.numberOfLines = 0;
_contentLabel.delegate =self;
_contentLabel;
})];
协议
DTAttributedTextContentViewDelegate,DTLazyImageViewDelegate
代理详情
#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];
imageView.url = attachment.contentURL;
imageView.delegate = self;
imageView.image = [(DTImageTextAttachment *)attachment image];
return imageView;
}
return nil;
}
#pragma mark - DTLazyImageViewDelegate
MARK: - 根据获取图片size重新加载
- (