Files
yinmeng-ios/xplan-ios/Main/Room/View/MessageContainerView/Tool/XPRoomMessageAttributeHelper.m
2021-11-04 15:05:31 +08:00

148 lines
7.1 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>
///Model
#import "XPMessageRemoteExtModel.h"
#import "ThemeColor+Room.h"
#import "XPRoomMessageConstant.h"
#import "NetImageView.h"
@implementation XPRoomMessageAttributeHelper
/// 文本类的消息的富文本
/// @param remoteModel 扩展字段中的内容
/// @param nick 名字
+ (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)];
[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) {
NSMutableAttributedString * experAttribute = [self createUrlImageAttribute:remoteModel.experUrl size:CGSizeMake(25, 16)];
[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 {
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 * genderString = [NSMutableAttributedString yy_attachmentStringWithContent:imageView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(imageView.frame.size.width, imageView.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;
}
@end