自加载网络图片的YYLabel

This commit is contained in:
zu
2021-11-02 22:55:39 +08:00
parent af6d90dcd2
commit 240dd0b894
11 changed files with 154 additions and 105 deletions

View File

@@ -9,6 +9,8 @@
/* Begin PBXBuildFile section */
180806D827293794001FD836 /* NSObject+MJExtension.m in Sources */ = {isa = PBXBuildFile; fileRef = 180806D727293794001FD836 /* NSObject+MJExtension.m */; };
180806FB2729A354001FD836 /* ThemeColor+Room.m in Sources */ = {isa = PBXBuildFile; fileRef = 180806FA2729A354001FD836 /* ThemeColor+Room.m */; };
1808072D2731598F001FD836 /* NetImageYYLabel.m in Sources */ = {isa = PBXBuildFile; fileRef = 1808072C2731598F001FD836 /* NetImageYYLabel.m */; };
1808073027315E8E001FD836 /* NetImageView.m in Sources */ = {isa = PBXBuildFile; fileRef = 1808072F27315E8E001FD836 /* NetImageView.m */; };
181D7F1B2726CE2A00B7C059 /* StageView.m in Sources */ = {isa = PBXBuildFile; fileRef = 181D7F1A2726CE2A00B7C059 /* StageView.m */; };
181D7F212727D9DB00B7C059 /* SocialStageView.m in Sources */ = {isa = PBXBuildFile; fileRef = 181D7F202727D9DB00B7C059 /* SocialStageView.m */; };
18486213271EA9DA005FC5DC /* RtcManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 18486212271EA9DA005FC5DC /* RtcManager.m */; };
@@ -276,6 +278,10 @@
180806F827298F9B001FD836 /* RoomGuestDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RoomGuestDelegate.h; sourceTree = "<group>"; };
180806F92729A354001FD836 /* ThemeColor+Room.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ThemeColor+Room.h"; sourceTree = "<group>"; };
180806FA2729A354001FD836 /* ThemeColor+Room.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "ThemeColor+Room.m"; sourceTree = "<group>"; };
1808072B2731598F001FD836 /* NetImageYYLabel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NetImageYYLabel.h; sourceTree = "<group>"; };
1808072C2731598F001FD836 /* NetImageYYLabel.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NetImageYYLabel.m; sourceTree = "<group>"; };
1808072E27315E8E001FD836 /* NetImageView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NetImageView.h; sourceTree = "<group>"; };
1808072F27315E8E001FD836 /* NetImageView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NetImageView.m; sourceTree = "<group>"; };
181D7F192726CE2A00B7C059 /* StageView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = StageView.h; sourceTree = "<group>"; };
181D7F1A2726CE2A00B7C059 /* StageView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = StageView.m; sourceTree = "<group>"; };
181D7F1F2727D9DB00B7C059 /* SocialStageView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SocialStageView.h; sourceTree = "<group>"; };
@@ -736,6 +742,15 @@
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
1808072727314F12001FD836 /* YYLabel */ = {
isa = PBXGroup;
children = (
1808072B2731598F001FD836 /* NetImageYYLabel.h */,
1808072C2731598F001FD836 /* NetImageYYLabel.m */,
);
path = YYLabel;
sourceTree = "<group>";
};
18486210271EA9A5005FC5DC /* RTC */ = {
isa = PBXGroup;
children = (
@@ -1041,6 +1056,7 @@
189DD56C26DF5B5400AB55B1 /* UI */ = {
isa = PBXGroup;
children = (
1808072727314F12001FD836 /* YYLabel */,
E8659640270160F200846EBD /* VagueImageView */,
E8AC720A26F435AF007D6E91 /* UIImageView */,
E81366F926F0D27A0076364C /* UIButton */,
@@ -1590,6 +1606,8 @@
E8AC720C26F435F5007D6E91 /* UIImageView+LoadImage.m */,
E8AC720E26F43955007D6E91 /* UIImageConstant.h */,
E8AC720F26F43955007D6E91 /* UIImageConstant.m */,
1808072E27315E8E001FD836 /* NetImageView.h */,
1808072F27315E8E001FD836 /* NetImageView.m */,
);
path = UIImageView;
sourceTree = "<group>";
@@ -2159,6 +2177,7 @@
189DD73F26E21C3F00AB55B1 /* YYUtility+Carrier.m in Sources */,
18E7B26926E8D5D60064BC9B /* XCCurrentVCStackManager.m in Sources */,
E824544326F58FCE00BE8163 /* XPMinePayPwdInputView.m in Sources */,
1808073027315E8E001FD836 /* NetImageView.m in Sources */,
18486235271EB794005FC5DC /* AgoraRtcImpl.m in Sources */,
184862CE27213FD7005FC5DC /* ZegoRtcImpl.m in Sources */,
9B0E1C5926E77022005D4442 /* BaseNavigationController.m in Sources */,
@@ -2230,6 +2249,7 @@
E8EEB90926FC579A007C6EBA /* XPMineUserInfoEditTableViewCell.m in Sources */,
18C17A5D26F338F300C48E11 /* XplanFBFlutterViewContainer.m in Sources */,
E89DA67527009ACD008483C1 /* XPMineRechargeNavView.m in Sources */,
1808072D2731598F001FD836 /* NetImageYYLabel.m in Sources */,
18486213271EA9DA005FC5DC /* RtcManager.m in Sources */,
186A536926FC6F2E00D67B2C /* XCShareView.m in Sources */,
186A534C26FC6ED900D67B2C /* TTAlertView.m in Sources */,

View File

@@ -0,0 +1,19 @@
//
// NetImageView.h
// xplan-ios
//
// Created by apple on 2021/11/2.
//
#import <UIKit/UIKit.h>
#import "UIImageView+LoadImage.h"
NS_ASSUME_NONNULL_BEGIN
@interface NetImageView : UIImageView
@property (nonatomic, copy) NSString * imgUrl;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,13 @@
//
// NetImageView.m
// xplan-ios
//
// Created by apple on 2021/11/2.
//
#import "NetImageView.h"
@implementation NetImageView
@end

View File

@@ -0,0 +1,18 @@
//
// NetImageYYLabel.h
// xplan-ios
//
// Created by zu on 2021/11/2.
//
#import <YYText/YYText.h>
NS_ASSUME_NONNULL_BEGIN
@interface NetImageYYLabel : YYLabel
- (void)loadAttributedText:(NSAttributedString *)attributedText load:(BOOL)load;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,41 @@
//
// NetImageYYLabel.m
// xplan-ios
//
// Created by zu on 2021/11/2.
//
#import "NetImageYYLabel.h"
#import "NetImageView.h"
@implementation NetImageYYLabel
- (void)loadAttributedText:(NSAttributedString *)attributedText load:(BOOL)load {
[super setAttributedText:attributedText];
if (load ) {
NSUInteger length = attributedText.length;
for (int i = 0; i < length; i++) {
NSRange range = NSMakeRange(0, 0);
NSDictionary* attr = [attributedText attributesAtIndex:i effectiveRange:&range];
YYTextAttachment* attachment = attr[YYTextAttachmentAttributeName];
if (!attachment || ![attachment.content isKindOfClass:[NetImageView class]]) continue;
NetImageView* imageView = attachment.content;
if (!imageView.imgUrl) continue;
[imageView load_imageWithUrl:imageView.imgUrl placeholderImage:[UIImageConstant defaultAvatarPlaceholder] type:ImageTypeUserIcon complection:^(UIImage * _Nonnull image, NSURL * _Nonnull url) {
CGFloat kscale = (CGFloat)imageView.image.size.width / (CGFloat)imageView.image.size.height; //
imageView.frame = CGRectMake(0, 0, imageView.frame.size.height * kscale, imageView.frame.size.height);
NSMutableAttributedString * replaceAttr = [NSMutableAttributedString yy_attachmentStringWithContent:imageView contentMode:UIViewContentModeScaleAspectFit attachmentSize:imageView.frame.size alignToFont:[UIFont systemFontOfSize:14.0] alignment:YYTextVerticalAlignmentCenter];
NSMutableAttributedString * attribute = [self.attributedText mutableCopy];
[attribute replaceCharactersInRange:range withAttributedString:replaceAttr];
[super setAttributedText:nil];
[super setAttributedText:attribute];
}];
}
}
}
@end

View File

@@ -11,21 +11,15 @@ NS_ASSUME_NONNULL_BEGIN
@class XPRoomMessageDisplayModel;
typedef void(^LoadUrlImageComplection)(UIImageView *imageView, XPRoomMessageDisplayModel * model);
@interface XPRoomMessageDisplayModel : NSObject
///消息实体
@property (nonatomic,strong, readonly) NIMMessage *message;
///索要展示的cell的高度
@property (nonatomic,assign, readonly) CGFloat cellHeight;
///所要展示的富文本
@property (nonatomic,strong, readonly) NSMutableAttributedString *contentAttributed;
@property (nonatomic,strong, readonly) NSMutableAttributedString *contentAttribute;
///选择了某个item
@property (nonatomic,copy) void(^didSelectItem)(NSString * uid);
///加载网络图片成功
@property (nonatomic,copy) LoadUrlImageComplection loadUrlComplection;
- (instancetype)initDisplayModel:(NIMMessage *)message;

View File

@@ -22,7 +22,7 @@
///cell
@property (nonatomic,assign) CGFloat cellHeight;
///
@property (nonatomic,strong) NSMutableAttributedString *contentAttributed;
@property (nonatomic,strong) NSMutableAttributedString *contentAttribute;
@end
@implementation XPRoomMessageDisplayModel
@@ -35,7 +35,6 @@
return self;
}
- (void)makeMessageAttribute:(NIMMessage *)message {
NIMMessageType messageType = message.messageType;
switch (messageType) {
@@ -64,14 +63,9 @@
nick = @"我:";
}
@kWeakify(self);
self.contentAttributed = [XPRoomMessageAttributeHelper createLevleTextAttribute:model nick:nick text:message.text complection:^(UIImageView * _Nonnull imageView) {
@kStrongify(self);
if (self.loadUrlComplection) {
self.loadUrlComplection(imageView, self);
}
}];
self.contentAttribute = [XPRoomMessageAttributeHelper createLevelTextAttribute:model nick:nick text:message.text];
[self.contentAttributed yy_setTextHighlightRange:NSMakeRange(0, self.contentAttributed.length) color:nil backgroundColor:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
[self.contentAttribute yy_setTextHighlightRange:NSMakeRange(0, self.contentAttribute.length) color:nil backgroundColor:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
@kStrongify(self);
if (self.didSelectItem) {
self.didSelectItem(message.from);
@@ -83,7 +77,7 @@
/// tips
/// @param message
- (void)makeTipsAttribute:(NIMMessage *)message {
self.contentAttributed = [XPRoomMessageAttributeHelper createTextAttribute:message.text titleColor:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont];
self.contentAttribute = [XPRoomMessageAttributeHelper createTextAttribute:message.text titleColor:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont];
}
///
@@ -95,9 +89,9 @@
switch (content.eventType) {
case NIMChatroomEventTypeEnter:///
{
self.contentAttributed = [XPRoomMessageAttributeHelper createUserEnterRoomAttribute:member.nick];
self.contentAttribute = [XPRoomMessageAttributeHelper createUserEnterRoomAttribute:member.nick];
@kWeakify(self);
[self.contentAttributed yy_setTextHighlightRange:NSMakeRange(0, self.contentAttributed.length) color:nil backgroundColor:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
[self.contentAttribute yy_setTextHighlightRange:NSMakeRange(0, self.contentAttribute.length) color:nil backgroundColor:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
@kStrongify(self);
if (self.didSelectItem) {
self.didSelectItem(message.from);
@@ -119,11 +113,11 @@
}
#pragma mark - Getters And Setters
- (void)setContentAttributed:(NSMutableAttributedString *)contentAttributed {
_contentAttributed = contentAttributed;
if (_contentAttributed.length > 0) {
- (void)setContentAttribute:(NSMutableAttributedString *)contentAttributed {
_contentAttribute = contentAttributed;
if (_contentAttribute.length > 0) {
CGSize maxSize = CGSizeMake(kRoomMessageMaxWidth, MAXFLOAT);
YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:maxSize text:_contentAttributed];
YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:maxSize text:_contentAttribute];
self.cellHeight = layout.textBoundingSize.height + kRoomMessageTextSpaceHeight * 2 + 10;
}
}

View File

@@ -8,7 +8,6 @@
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
typedef void(^AttributeLoadImageComplection)(UIImageView *);
@class XPMessageRemoteExtModel;
@interface XPRoomMessageAttributeHelper : NSObject
@@ -16,7 +15,7 @@ typedef void(^AttributeLoadImageComplection)(UIImageView *);
/// 文本类的消息的富文本 NIMMessageTypeText
/// @param remoteModel 扩展字段中的内容
/// @param nick 名字
+ (NSMutableAttributedString *)createLevleTextAttribute:(XPMessageRemoteExtModel *)remoteModel nick:(NSString *)nick text:(NSString *)text complection:(nullable AttributeLoadImageComplection)complection;
+ (NSMutableAttributedString *)createLevelTextAttribute:(XPMessageRemoteExtModel *)remoteModel nick:(NSString *)nick text:(NSString *)text;
/// 进入房间的富文本 NIMChatroomEventTypeEnter
/// @param nick 进入房间的名称
@@ -27,12 +26,6 @@ typedef void(^AttributeLoadImageComplection)(UIImageView *);
/// @param size 网络图片的大小
+ (NSMutableAttributedString *)createUrlImageAttribute:(NSString *)imageUrl size:(CGSize)size;
/// 生成一个图片的富文本
/// @param imageUrl 网络图片的地址
/// @param size 网络图片的大小
/// @param complection 成功 可以拿到图片的大小刷新富文本的大小
+ (NSMutableAttributedString *)createUrlImageAttribute:(NSString *)imageUrl size:(CGSize)size complection:(nullable AttributeLoadImageComplection)complection;
/// 生成本地一个图片的富文本
/// @param imageName 网络图片的地址
/// @param size 网络图片的大小

View File

@@ -9,18 +9,18 @@
///Third
#import <YYText/YYText.h>
#import <SDWebImage/SDWebImage.h>
///Tool
#import "UIImageView+LoadImage.h"
///Model
#import "XPMessageRemoteExtModel.h"
#import "ThemeColor+Room.h"
#import "XPRoomMessageConstant.h"
#import "NetImageView.h"
@implementation XPRoomMessageAttributeHelper
///
/// @param remoteModel
/// @param nick
+ (NSMutableAttributedString *)createLevleTextAttribute:(XPMessageRemoteExtModel *)remoteModel nick:(NSString *)nick text:(NSString *)text complection:(void (^)(UIImageView *))complection {
+ (NSMutableAttributedString *)createLevelTextAttribute:(XPMessageRemoteExtModel *)remoteModel nick:(NSString *)nick text:(NSString *)text {
NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] init];
if (remoteModel.defUser == UserLevelType_Offical) {
NSMutableAttributedString * offcialAttribute = [self createLocalImageAttribute:@"common_offical" size:CGSizeMake(13, 13)];
@@ -41,11 +41,7 @@
CGFloat kscale = image.size.width / image.size.height;
experAttribute = [self createImageAttribute:image size:CGSizeMake(16 * kscale, 16)];
} else {
experAttribute = [self createUrlImageAttribute:remoteModel.experUrl size:CGSizeMake(25, 16) complection:^(UIImageView * _Nonnull imageView) {
if (complection) {
complection(imageView);
}
}];
experAttribute = [self createUrlImageAttribute:remoteModel.experUrl size:CGSizeMake(25, 16)];
}
[attribute appendAttributedString:experAttribute];
[attribute appendAttributedString:[self createSapceAttribute:2]];
@@ -81,22 +77,8 @@
/// @param imageUrl
/// @param size
+ (NSMutableAttributedString *)createUrlImageAttribute:(NSString *)imageUrl size:(CGSize)size {
return [self createUrlImageAttribute:imageUrl size:size complection:nil];
}
///
/// @param imageUrl
/// @param size
/// @param complection
+ (NSMutableAttributedString *)createUrlImageAttribute:(NSString *)imageUrl size:(CGSize)size complection:(nullable AttributeLoadImageComplection)complection {
UIImageView *genderImageView = [[UIImageView alloc]init];
[genderImageView load_imageWithUrl:imageUrl placeholderImage:[UIImageConstant defaultAvatarPlaceholder] type:ImageTypeUserIcon complection:^(UIImage * _Nonnull image, NSURL * _Nonnull url) {
if (image && complection) {
genderImageView.image = image;
complection(genderImageView);
}
}];
NetImageView *genderImageView = [[NetImageView alloc]init];
genderImageView.imgUrl = imageUrl;
genderImageView.bounds = CGRectMake(0, 0, 25, 16);
genderImageView.layer.masksToBounds = YES;

View File

@@ -7,13 +7,16 @@
#import <UIKit/UIKit.h>
@class NetImageYYLabel;
NS_ASSUME_NONNULL_BEGIN
@class XPRoomMessageDisplayModel, YYLabel;
@interface XPRoomMessageTableViewCell : UITableViewCell
///展示的内容
@property (nonatomic,strong, readonly) YYLabel *contentLabel;
///
@property (nonatomic,strong) XPRoomMessageDisplayModel *displayModel;
@end
NS_ASSUME_NONNULL_END

View File

@@ -8,7 +8,7 @@
#import "XPRoomMessageTableViewCell.h"
///Third
#import <Masonry/Masonry.h>
#import <YYText/YYText.h>
#import "NetImageYYLabel.h"
///Tool
#import "XPMacro.h"
#import "ThemeColor+Room.h"
@@ -19,9 +19,9 @@
@interface XPRoomMessageTableViewCell ()
///
@property (nonatomic,strong) UIImageView *bubbleImaegeView;
@property (nonatomic,strong) UIImageView *bubbleImageView;
///
@property (nonatomic,strong) YYLabel *contentLabel;
@property (nonatomic,strong) NetImageYYLabel *contentLabel;
@end
@implementation XPRoomMessageTableViewCell
@@ -38,75 +38,47 @@
- (void)initSubViews {
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:self.bubbleImaegeView];
[self.bubbleImaegeView addSubview:self.contentLabel];
[self.contentView addSubview:self.bubbleImageView];
[self.bubbleImageView addSubview:self.contentLabel];
}
- (void)initSubViewConstraints {
[self.bubbleImaegeView mas_makeConstraints:^(MASConstraintMaker *make) {
[self.bubbleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.mas_equalTo(self.contentView);
make.bottom.mas_equalTo(self.contentView).offset(-10);
make.right.mas_equalTo(self.contentLabel).offset(5);
}];
[self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.bubbleImaegeView).offset(5);
make.centerY.mas_equalTo(self.bubbleImaegeView);
make.left.mas_equalTo(self.bubbleImageView).offset(5);
make.centerY.mas_equalTo(self.bubbleImageView);
}];
}
- (void)loadUrlImageSuccess:(UIImageView *)imageView XPRoomMessageDisplayModel:(XPRoomMessageDisplayModel *)model {
if (self.displayModel == model) {
CGSize size = CGSizeMake(kRoomMessageMaxWidth, CGFLOAT_MAX);
YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:size text:self.contentLabel.attributedText];
NSMutableAttributedString * attribute = [self.contentLabel.attributedText mutableCopy];
if (layout.attachments.count > 0) {
for (int i = 0; i < layout.attachments.count; i++) {
YYTextAttachment * attachment = [layout.attachments objectAtIndex:i];
if (imageView == attachment.content) {
CGFloat kscale = (CGFloat)imageView.image.size.width / (CGFloat)imageView.image.size.height;
imageView.frame = CGRectMake(0, 0, 16 * kscale, 16);
NSValue * value = [layout.attachmentRanges objectAtIndex:i];
NSRange range = value.rangeValue;
NSMutableAttributedString * resultString = [NSMutableAttributedString yy_attachmentStringWithContent:imageView contentMode:UIViewContentModeScaleAspectFit attachmentSize:imageView.frame.size alignToFont:[UIFont systemFontOfSize:14.0] alignment:YYTextVerticalAlignmentCenter];
[attribute replaceCharactersInRange:range withAttributedString:resultString];
break;
}
}
self.contentLabel.attributedText = nil;
self.contentLabel.attributedText = attribute;
}
}
}
#pragma mark - Getters And Setters
- (void)setDisplayModel:(XPRoomMessageDisplayModel *)displayModel {
_displayModel = displayModel;
self.contentLabel.attributedText = displayModel.contentAttributed;
[self.contentLabel loadAttributedText:displayModel.contentAttribute load:YES];
displayModel.didSelectItem = ^(NSString * _Nonnull uid) {
/// TODO :
NSLog(@"%@", uid);
};
displayModel.loadUrlComplection = ^(UIImageView * _Nonnull imageView, XPRoomMessageDisplayModel * _Nonnull model) {
[self loadUrlImageSuccess:imageView XPRoomMessageDisplayModel:model];
};
}
- (UIImageView *)bubbleImaegeView {
if (!_bubbleImaegeView) {
_bubbleImaegeView = [[UIImageView alloc] init];
_bubbleImaegeView.userInteractionEnabled = YES;
_bubbleImaegeView.layer.masksToBounds = YES;
_bubbleImaegeView.layer.cornerRadius = 7;
_bubbleImaegeView.image = [UIImage imageWithColor:[ThemeColor messageBubbleColor]];
- (UIImageView *)bubbleImageView {
if (!_bubbleImageView) {
_bubbleImageView = [[UIImageView alloc] init];
_bubbleImageView.userInteractionEnabled = YES;
_bubbleImageView.layer.masksToBounds = YES;
_bubbleImageView.layer.cornerRadius = 7;
_bubbleImageView.image = [UIImage imageWithColor:[ThemeColor messageBubbleColor]];
}
return _bubbleImaegeView;
return _bubbleImageView;
}
- (YYLabel *)contentLabel {
- (NetImageYYLabel *)contentLabel {
if (!_contentLabel) {
_contentLabel = [[YYLabel alloc] init];
_contentLabel = [[NetImageYYLabel alloc] init];
_contentLabel.preferredMaxLayoutWidth = kRoomMessageMaxWidth - 10;
_contentLabel.numberOfLines = 0;
}