Files
yinmeng-ios/xplan-ios/Main/Room/View/MessageContainerView/Tool/XPRoomMessageAttributeHelper.m
2021-10-29 15:53:44 +08:00

238 lines
9.5 KiB
Objective-C

//
// XPRoomMessageHelper.m
// xplan-ios
//
// Created by 冯硕 on 1621/10/26.
//
#import "XPRoomMessageAttributeHelper.h"
///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"
@implementation XPRoomMessageAttributeHelper
/// 文本类的消息的富文本
/// @param remoteModel 扩展字段中的内容
/// @param nick 名字
+ (NSMutableAttributedString *)createLevleTextAttribute:(XPMessageRemoteExtModel *)remoteModel nick:(NSString *)nick text:(NSString *)text complection:(void (^)(UIImageView *))complection {
NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] init];
if (remoteModel.defUser == UserLevelType_Offical) {
NSMutableAttributedString * offcialAttribute = [self createLocalImageAttribute:@"common_offical" size:CGSizeMake(13, 13)];
[attribute appendAttributedString:offcialAttribute];
[attribute appendAttributedString:[self createSapceAttribute:2]];
}
if (remoteModel.newUser) {
NSMutableAttributedString * newUserAttribute = [self createLocalImageAttribute:@"common_new_user" size:CGSizeMake(13, 13)];
[attribute appendAttributedString:newUserAttribute];
[attribute appendAttributedString:[self createSapceAttribute:2]];
}
if (remoteModel.experUrl) {
UIImage * image = [self cacheImage:remoteModel.experUrl type:ImageTypeUserIcon];
NSMutableAttributedString * experAttribute;
if (image) {
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);
}
}];
}
[attribute appendAttributedString:experAttribute];
[attribute appendAttributedString:[self createSapceAttribute:2]];
}
NSMutableAttributedString * nickAttribute = [self createTextAttribute:nick titleColor:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont];
[attribute appendAttributedString:nickAttribute];
[attribute appendAttributedString:[self createSapceAttribute:2]];
NSMutableAttributedString * textAttribute = [self createTextAttribute:text titleColor:[ThemeColor messageTextColor] font:kRoomMessageDefalutFont];
[attribute appendAttributedString:textAttribute];
return attribute;
}
/// 进入房间的富文本 NIMChatroomEventTypeEnter
/// @param nick 进入房间的名称
+ (NSMutableAttributedString *)createUserEnterRoomAttribute:(NSString *)nick {
nick = nick.length > 0 ? nick : @"";
NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] init];
NSMutableAttributedString * nickAttribute = [self createTextAttribute:nick titleColor:[ThemeColor messageNickColor] font:kRoomMessageDefalutFont];
NSMutableAttributedString * enterRoomAttribute = [self createTextAttribute:@"进入了房间" titleColor:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont];
[attribute appendAttributedString:nickAttribute];
[attribute appendAttributedString:enterRoomAttribute];
return attribute;
}
#pragma mark - 富文本的工具类
/// 生成一个图片的富文本
/// @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);
}
}];
genderImageView.bounds = CGRectMake(0, 0, 25, 16);
genderImageView.layer.masksToBounds = YES;
genderImageView.contentMode = UIViewContentModeScaleAspectFill;
NSMutableAttributedString * genderString = [NSMutableAttributedString yy_attachmentStringWithContent:genderImageView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(genderImageView.frame.size.width, genderImageView.frame.size.height) alignToFont:[UIFont systemFontOfSize:15.0] alignment:YYTextVerticalAlignmentCenter];
return genderString;
}
/// 生成本地一个图片的富文本
/// @param imageName 网络图片的地址
/// @param size 网络图片的大小
+ (NSMutableAttributedString *)createLocalImageAttribute:(NSString *)imageName size:(CGSize)size {
UIImageView *genderImageView = [[UIImageView alloc]init];
genderImageView.image = [UIImage imageNamed:imageName];
CGFloat kscale = (CGFloat)genderImageView.image.size.width / (CGFloat)genderImageView.image.size.height;
genderImageView.bounds = CGRectMake(0, 0, 16 * kscale, 16);
NSMutableAttributedString * genderString = [NSMutableAttributedString yy_attachmentStringWithContent:genderImageView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(genderImageView.frame.size.width, genderImageView.frame.size.height) alignToFont:[UIFont systemFontOfSize:15.0] alignment:YYTextVerticalAlignmentCenter];
return genderString;
}
/// 生成本地一个图片的富文本
/// @param image 本地的图片
/// @param size 本地图片的大小
+ (NSMutableAttributedString *)createImageAttribute:(UIImage *)image size:(CGSize)size {
UIImageView *genderImageView = [[UIImageView alloc]init];
genderImageView.image = image;
CGFloat kscale = (CGFloat)genderImageView.image.size.width / (CGFloat)genderImageView.image.size.height;
genderImageView.bounds = CGRectMake(0, 0, 16 * kscale,16);
NSMutableAttributedString * genderString = [NSMutableAttributedString yy_attachmentStringWithContent:genderImageView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(genderImageView.frame.size.width, genderImageView.frame.size.height) alignToFont:[UIFont systemFontOfSize:15.0] alignment:YYTextVerticalAlignmentCenter];
return genderString;
}
/// 生成一个富文本
/// @param text 富文本的文字
/// @param titleColor 文字的颜色
/// @param font 文字的大小
+ (NSMutableAttributedString *)createTextAttribute:(NSString *)text titleColor:(UIColor *)titleColor font:(CGFloat)font {
NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:text attributes:nil];
attribute.yy_font = [UIFont systemFontOfSize:font];
attribute.yy_color = titleColor;
attribute.yy_paragraphStyle = [self paragraphStyle];
return attribute;
}
/// 设置文本的样式 间隙 缩进 ...
+ (NSMutableParagraphStyle *)paragraphStyle {
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.lineSpacing = 4.0f;//行间距
// 强制排版(从左到右)
paraStyle.alignment = NSTextAlignmentLeft;
paraStyle.baseWritingDirection = NSWritingDirectionLeftToRight;
return paraStyle;
}
/// 占位的富文本
/// @param width 需要的间隙
+ (NSMutableAttributedString *)createSapceAttribute:(CGFloat)width {
UIView *spaceView = [[UIView alloc]init];
spaceView.backgroundColor = [UIColor clearColor];
spaceView.bounds = CGRectMake(0, 0, width,10);
NSMutableAttributedString * genderString = [NSMutableAttributedString yy_attachmentStringWithContent:spaceView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(spaceView.frame.size.width, spaceView.frame.size.height) alignToFont:[UIFont systemFontOfSize:15.0] alignment:YYTextVerticalAlignmentCenter];
return genderString;
}
#pragma mark - Private Method
+ (UIImage *)cacheImage:(NSString *)urlStr type:(ImageType)type {
NSString * key = [self getCacheImageKey:urlStr type:type];
// 缓存的图片(内存)
UIImage *image = [[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:key];
// 缓存的图片(硬盘)
if (!image) {
image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:key];
}
return image;
}
+ (NSString *)getCacheImageKey:(NSString *)imageUrl type:(ImageType)type {
NSMutableString *urlString = [NSMutableString stringWithString:imageUrl];
NSString * configUrl;
switch (type) {
case ImageTypeUserIcon:
{
configUrl = kImageTypeUserIcon;
}
break;
case ImageTypeCornerAvatar:
{
configUrl = kImageTypeCornerAvatar;
}
break;
case ImageTypeRoomFace:
{
configUrl = kImageTypeRoomFace;
}
break;
case ImageTypeUserLibaryDetail:
{
configUrl = kImageTypeUserLibaryDetail;
}
break;
case ImageTypeRoomGift:
{
configUrl = kImageTypeRoomGift;
}
break;
case ImageTypeUserInfoAlbum:
{
configUrl = kImageTypeUserInfoAlbum;
}
break;
default:
break;
}
if (configUrl) {
if ([imageUrl containsString:@"?"]) {
[urlString appendString:@"|"];
}else{
[urlString appendString:@"?"];
}
[urlString appendString:configUrl];
}
NSString *encodedString = (NSString *)
CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)urlString,
NULL,
(CFStringRef)@"|",
kCFStringEncodingUTF8));
return encodedString;
}
@end