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重新加载
- (void)lazyImageView:(DTLazyImageView *)lazyImageView didChangeImageSize:(CGSize)size
{
CGFloat width = SCREEN_WIDTH-kWidth(40);//自己设定的宽
CGSize imageSize = size;
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;
for (DTTextAttachment *oneAttachment in [self.contentLabel.layoutFrame textAttachmentsWithPredicate:pred]) {
if (CGSizeEqualToSize(oneAttachment.displaySize, CGSizeZero)) {
oneAttachment.displaySize = imageSize;
[self configNoSizeImageView:url.absoluteString size:imageSize];
didUpdate = YES;
}
}
if (didUpdate) {
CGRect maxRect = CGRectMake(kWidth(20), kWidth(20), SCREEN_WIDTH-kWidth(40), CGFLOAT_HEIGHT_UNKNOWN);
CGSize textSize = [self getAttributedTextHeightHtml:self.viewModel.eventsItem.content with_viewMaxRect:maxRect];
self.contentLabel.attributedString = [self getAttributedStringWithHtml:self.viewModel.eventsItem.content];
[self.contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.detailLabel.mas_bottom).offset = kHeigh(16);
make.leading.offset = kWidth(20);
make.trailing.offset = -kWidth(20);
make.height.mas_equalTo(textSize.height);
}];
}
}
#pragma mark - private Methods
//使用得到的新图片尺寸,更新HtmlString字符串
- (void)configNoSizeImageView:(NSString *)url size:(CGSize)size{
//_viewMaxRect是预设的最大Frame
CGRect maxRect = CGRectMake(kWidth(20), kWidth(20), SCREEN_WIDTH-kWidth(40), CGFLOAT_HEIGHT_UNKNOWN);
CGFloat imgSizeScale = size.height/size.width;
CGFloat widthPx = maxRect.size.width;
CGFloat heightPx = widthPx * imgSizeScale;
NSString *imageInfo = [NSString stringWithFormat:@"src=\"%@\"",url];
NSString *sizeString = [NSString stringWithFormat:@"style=\"width:%.fpx; height:%.fpx;\"",widthPx,heightPx];
NSString *newImageInfo = [NSString stringWithFormat:@"src=\"%@\"%@",url,sizeString];
if ([self.viewModel.eventsItem.content containsString:imageInfo]) {
NSString *newHtml = [self.viewModel.eventsItem.content stringByReplacingOccurrencesOfString:imageInfo withString:newImageInfo];
self.viewModel.eventsItem.content = newHtml;
}
}
//使用HtmlString,和预设的Frame,计算富文本视图自适应后的高度
- (CGSize)getAttributedTextHeightHtml:(NSString *)htmlString with_viewMaxRect:(CGRect)_viewMaxRect{
//获取富文本
NSAttributedString *attributedString = [self getAttributedStringWithHtml:htmlString];
//获取布局器
DTCoreTextLayouter *layouter = [[DTCoreTextLayouter alloc] initWithAttributedString:attributedString];
NSRange entireString = NSMakeRange(0, [attributedString length]);
//获取Frame
DTCoreTextLayoutFrame *layoutFrame = [layouter layoutFrameWithRect:_viewMaxRect range:entireString];
//得到大小
CGSize sizeNeeded = [layoutFrame frame].size;
return sizeNeeded;
}
//Html->富文本NSAttributedString
- (NSAttributedString *)getAttributedStringWithHtml:(NSString *)htmlString{
//获取富文本
NSData *data = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithHTMLData:data documentAttributes:NULL];
NSRange range = NSMakeRange(0, attributedString.length);
if (range.length>0) {
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:0];//调整行间距
paragraphStyle.maximumLineHeight = kScale(22);
paragraphStyle.minimumLineHeight = kScale(22);
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
[attributedString addAttribute:NSForegroundColorAttributeName value:XAsset.color_joyEventsDetailContent range:range];
[attributedString addAttribute:NSFontAttributeName value:XAsset.font_joyEventsDetailContent range:range];
}
return attributedString;
}