186 lines
7.9 KiB
Objective-C
186 lines
7.9 KiB
Objective-C
//
|
|
// XPRoomMessageParser.m
|
|
// xplan-ios
|
|
//
|
|
// Created by 冯硕 on 2021/10/26.
|
|
//
|
|
|
|
#import "XPRoomMessageParser.h"
|
|
#import <YYText/YYText.h>
|
|
#import <NIMSDK/NIMSDK.h>
|
|
///Tool
|
|
#import "ThemeColor+Room.h"
|
|
#import "XPRoomMessageConstant.h"
|
|
#import "AccountInfoStorage.h"
|
|
#import "XPMacro.h"
|
|
///Model
|
|
#import "XPMessageRemoteExtModel.h"
|
|
|
|
#import "NetImageView.h"
|
|
|
|
@implementation XPRoomMessageParser
|
|
|
|
+ (NSAttributedString*)parseMessageAttribute:(NIMMessage *)message {
|
|
NIMMessageType messageType = message.messageType;
|
|
switch (messageType) {
|
|
case NIMMessageTypeText:
|
|
return [self makeChatAttribute:message];
|
|
|
|
case NIMMessageTypeTip:
|
|
return [self makeTipsAttribute:message];
|
|
|
|
case NIMMessageTypeNotification:
|
|
return [self makeNotificationAttribute:message];
|
|
default:
|
|
return nil;
|
|
}
|
|
}
|
|
|
|
/// 用户公屏聊天
|
|
/// @param message 消息的实体
|
|
+ (NSAttributedString*)makeChatAttribute:(NIMMessage *)message{
|
|
NSString * uid = [AccountInfoStorage instance].getUid;
|
|
XPMessageRemoteExtModel * model = [XPMessageRemoteExtModel modelWithJSON:message.remoteExt[message.from]];
|
|
NSString * nick = [NSString stringWithFormat:@"%@:", ((NIMMessageChatroomExtension *)message.messageExt).roomNickname];
|
|
if ([message.from isEqualToString:uid]) {
|
|
nick = @"我:";
|
|
}
|
|
|
|
NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] init];
|
|
if (model.defUser == UserLevelType_Offical) {
|
|
NSMutableAttributedString * offcialAttribute = [self createLocalImageAttribute:@"common_offical"];
|
|
[attribute appendAttributedString:offcialAttribute];
|
|
[attribute appendAttributedString:[self createSapceAttribute:2]];
|
|
}
|
|
|
|
if (model.newUser) {
|
|
NSMutableAttributedString * newUserAttribute = [self createLocalImageAttribute:@"common_new_user"];
|
|
[attribute appendAttributedString:newUserAttribute];
|
|
[attribute appendAttributedString:[self createSapceAttribute:2]];
|
|
}
|
|
|
|
if (model.experUrl) {
|
|
NSMutableAttributedString * experAttribute = [self createUrlImageAttribute:model.experUrl];
|
|
[attribute appendAttributedString:experAttribute];
|
|
[attribute appendAttributedString:[self createSapceAttribute:2]];
|
|
}
|
|
|
|
NSMutableAttributedString * nickAttribute = [self createTextAttribute:nick color:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont];
|
|
[attribute appendAttributedString:nickAttribute];
|
|
[attribute appendAttributedString:[self createSapceAttribute:2]];
|
|
|
|
NSMutableAttributedString * textAttribute = [self createTextAttribute:message.text color:[ThemeColor messageTextColor] font:kRoomMessageDefalutFont];
|
|
[attribute appendAttributedString:textAttribute];
|
|
|
|
return attribute;
|
|
}
|
|
|
|
/// 房间tips消息
|
|
/// @param message 消息的实体
|
|
+ (NSAttributedString*)makeTipsAttribute:(NIMMessage *)message {
|
|
return [self createTextAttribute:message.text color:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont];
|
|
}
|
|
|
|
/// 房间通知类消息
|
|
/// @param message 消息的实体
|
|
+ (NSAttributedString*)makeNotificationAttribute:(NIMMessage *)message {
|
|
NIMNotificationObject *notiMsg = (NIMNotificationObject *)message.messageObject;
|
|
NIMChatroomNotificationContent *content = (NIMChatroomNotificationContent *)notiMsg.content;
|
|
NIMChatroomNotificationMember *member = content.targets[0];
|
|
switch (content.eventType) {
|
|
case NIMChatroomEventTypeEnter:///进入房间
|
|
{
|
|
NSString* nick = member.nick.length > 0 ? member.nick : @"";
|
|
NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] init];
|
|
NSMutableAttributedString * nickAttribute = [self createTextAttribute:nick color:[ThemeColor messageNickColor] font:kRoomMessageDefalutFont];
|
|
NSMutableAttributedString * enterRoomAttribute = [self createTextAttribute:@"进入了房间" color:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont];
|
|
[attribute appendAttributedString:nickAttribute];
|
|
[attribute appendAttributedString:enterRoomAttribute];
|
|
return attribute;
|
|
}
|
|
|
|
case NIMChatroomEventTypeInfoUpdated:///房间信息更新
|
|
return [self createRoomInfoUpdateAttribute:content.notifyExt];
|
|
|
|
default:
|
|
return nil;
|
|
}
|
|
}
|
|
|
|
///房间信息更新
|
|
+ (NSAttributedString*)createRoomInfoUpdateAttribute:(NSString *)notifyExt {
|
|
return nil;
|
|
}
|
|
|
|
/// 生成一个图片的富文本
|
|
/// @param imageUrl 网络图片的地址
|
|
+ (NSMutableAttributedString *)createUrlImageAttribute:(NSString *)imageUrl {
|
|
NetImageView *imageView = [[NetImageView alloc]init];
|
|
imageView.imageUrl = imageUrl;
|
|
|
|
UIImage* image = imageView.image;
|
|
if (image) {
|
|
CGFloat scale = image.size.width / image.size.height;
|
|
imageView.bounds = CGRectMake(0, 0, 16 * scale, 16);
|
|
} else {
|
|
imageView.bounds = CGRectMake(0, 0, 16, 16);
|
|
}
|
|
imageView.layer.masksToBounds = YES;
|
|
imageView.contentMode = UIViewContentModeScaleAspectFill;
|
|
NSMutableAttributedString * attrString = [NSMutableAttributedString yy_attachmentStringWithContent:imageView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(imageView.bounds.size.width, imageView.bounds.size.height) alignToFont:[UIFont systemFontOfSize:15.0] alignment:YYTextVerticalAlignmentCenter];
|
|
return attrString;
|
|
}
|
|
|
|
#pragma mark - private base methods
|
|
/// 生成本地一个图片的富文本
|
|
/// @param imageName 网络图片的地址
|
|
+ (NSMutableAttributedString *)createLocalImageAttribute:(NSString *)imageName {
|
|
return [self createImageAttribute:[UIImage imageNamed:imageName]];
|
|
}
|
|
|
|
/// 生成本地一个图片的富文本
|
|
/// @param image 本地的图片
|
|
+ (NSMutableAttributedString *)createImageAttribute:(UIImage *)image {
|
|
UIImageView *imaveView = [[UIImageView alloc]init];
|
|
imaveView.image = image;
|
|
CGFloat scale = (CGFloat)imaveView.image.size.width / (CGFloat)imaveView.image.size.height;
|
|
imaveView.bounds = CGRectMake(0, 0, 16 * scale, 16);
|
|
NSMutableAttributedString * attrString = [NSMutableAttributedString yy_attachmentStringWithContent:imaveView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(imaveView.frame.size.width, imaveView.frame.size.height) alignToFont:[UIFont systemFontOfSize:15.0] alignment:YYTextVerticalAlignmentCenter];
|
|
return attrString;
|
|
}
|
|
|
|
/// 生成一个富文本
|
|
/// @param text 富文本的文字
|
|
/// @param color 文字的颜色
|
|
/// @param font 文字的大小
|
|
+ (NSMutableAttributedString *)createTextAttribute:(NSString *)text color:(UIColor *)color font:(CGFloat)font {
|
|
NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:text attributes:nil];
|
|
attribute.yy_font = [UIFont systemFontOfSize:font];
|
|
attribute.yy_color = color;
|
|
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 * attribute = [NSMutableAttributedString yy_attachmentStringWithContent:spaceView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(spaceView.frame.size.width, spaceView.frame.size.height) alignToFont:[UIFont systemFontOfSize:15.0] alignment:YYTextVerticalAlignmentCenter];
|
|
return attribute;
|
|
}
|
|
|
|
@end
|