Files
peko-ios/YuMi/Modules/YMRoom/View/MessageContainerView/View/XPNetImageYYLabel.m

111 lines
4.6 KiB
Objective-C

//
// NetImageYYLabel.m
// YUMI
//
// Created by zu on 2021/11/2.
//
#import "XPNetImageYYLabel.h"
#import "NetImageView.h"
#import "XPRoomMessageConstant.h"
#import "NSArray+Safe.h"
@implementation XPNetImageYYLabel
- (void)setAttributedText:(NSAttributedString *)attributedText {
if (!attributedText) return;
NSMutableAttributedString* attributedTextCopy = [attributedText mutableCopy];
CGSize maxSize = CGSizeMake(kRoomMessageMaxWidth - 24, MAXFLOAT);
CGSize size = [attributedText boundingRectWithSize:maxSize
options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin
context:nil].size;
YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(size.width, MAXFLOAT)];
container.truncationType = YYTextTruncationTypeEnd;
YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:maxSize
text:attributedTextCopy];
for (NSUInteger i = 0; i < layout.attachments.count; i++) {
YYTextAttachment *attachment = [layout.attachments xpSafeObjectAtIndex:i];
if (![attachment.content isKindOfClass:[NetImageView class]]) {
continue;
}
NetImageView *imageView = (NetImageView *)attachment.content;
if (!imageView.imageUrl){
continue;
}
NSValue *value = [layout.attachmentRanges xpSafeObjectAtIndex:i];
if (!value) {
continue;
}
NSRange range = value.rangeValue;
if (imageView.state == NetImageStateLoaded) {
attributedTextCopy = [self updateNetImageAttribute:imageView
attributes:attributedTextCopy
range:range];
} else {
@kWeakify(self);
[imageView loadImage:^(UIImage * _Nonnull image, NSURL * _Nonnull url) {
@kStrongify(self);
@kWeakify(self);
dispatch_async(dispatch_get_main_queue(), ^{
@kStrongify(self);
[self updateImageAndRefresh:imageView
attributedText:attributedTextCopy
range:range];
});
}];
}
}
[super setAttributedText:attributedTextCopy];
}
- (void)updateImageAndRefresh:(NetImageView *)imageView
attributedText:(NSMutableAttributedString *)attributedText
range:(NSRange)range {
if (!imageView || !attributedText) return;
imageView.image = imageView.image ?: [UIImage new];
NSMutableAttributedString *updatedAttributes = [self updateNetImageAttribute:imageView
attributes:attributedText
range:range];
[super setAttributedText:updatedAttributes];
}
- (NSMutableAttributedString*)updateNetImageAttribute:(NetImageView*)imageView
attributes:(NSMutableAttributedString*)attributes
range:(NSRange)range{
if (!imageView || !attributes) {
return attributes;
}
CGSize size = imageView.bounds.size;
if (CGSizeEqualToSize(size, CGSizeZero)) {
size = CGSizeMake(kFontRegular(kRoomMessageDefalutFont).lineHeight,
kFontRegular(kRoomMessageDefalutFont).lineHeight);
}
UIImage *image = imageView.image;
if (image) {
CGFloat scale = image.size.width / image.size.height;
size = CGSizeMake(imageView.bounds.size.height * scale, imageView.bounds.size.height);
}
imageView.bounds = CGRectMake(0, 0, size.width, size.height);
NSMutableAttributedString *replaceAttr = [NSMutableAttributedString yy_attachmentStringWithContent:imageView
contentMode:UIViewContentModeScaleAspectFit
attachmentSize:size
alignToFont:kFontRegular(kRoomMessageDefalutFont)
alignment:YYTextVerticalAlignmentCenter];
[attributes replaceCharactersInRange:range withAttributedString:replaceAttr];
return attributes;
}
@end