Files
peko-ios/YuMi/Modules/YMMessage/View/Session/Content/SesssionModel/MessageHeadlinesTextModel.m

120 lines
5.6 KiB
Objective-C

//
// MessageHeadlinesTextModel.m
// YuMi
//
// Created by duoban on 2024/5/9.
//
#import "MessageHeadlinesTextModel.h"
#import "QEmotionHelper.h"
@implementation MessageHeadlinesTextModel
- (instancetype)initWithMessage:(NIMMessage *)message {
if (![message isKindOfClass:[NIMMessage class]] || ![message respondsToSelector:@selector(remoteExt)]) {
return nil;
}
if (self = [super initWithMessage:message]) {
self.messageType = SessionMessageType_Text;
NSDictionary *dic = message.remoteExt[message.from];
XPMessageRemoteExtModel *extModel = [XPMessageRemoteExtModel modelWithJSON:dic];
if ([[dic allKeys] containsObject:@"headwearType"]) {
extModel.headWearType = [dic[@"headwearType"] integerValue];
} else if ([[dic allKeys] containsObject:@"headWearType"]) {
extModel.headWearType = [dic[@"headWearType"] integerValue];
}
if (extModel.headWearUrl.length == 0) {
if ([[dic allKeys] containsObject:@"headwearUrl"]) {
extModel.headWearUrl = dic[@"headwearUrl"];
} else if ([[dic allKeys] containsObject:@"headWearUrl"]) {
extModel.headWearUrl = dic[@"headWearUrl"];
}
}
self.extModel = extModel;
self.isSelf = [[NIMSDK sharedSDK].loginManager.currentAccount isEqualToString:message.from];
NSString * messageText = message.text;
if (!messageText) {
// messageText = YMLocalizedString(@"MessageContentText0");
messageText = [NSString stringWithFormat:@"%@\n%@",
YMLocalizedString(@"MessageContentText0"),
message.rawAttachContent];
}
CGFloat width = [UILabel getWidthWithText:messageText height:kGetScaleWidth(36) font:kFontMedium(14)];
width = width > kGetScaleWidth(200) ? kGetScaleWidth(200) : width;
self.width = width >= kGetScaleWidth(220) ? kGetScaleWidth(220) : (width + kGetScaleWidth(25));
CGSize dstRect = CGSizeMake(width, MAXFLOAT);
QEmotionHelper *faceManager = [QEmotionHelper sharedEmotionHelper];
NSMutableAttributedString * attribute = [faceManager attributedStringByText:messageText font:kFontMedium(14) forMessageBubble:YES];
if(extModel.iosBubbleUrl.length > 0){
[attribute addAttributes:@{NSForegroundColorAttributeName: UIColorFromRGB(0x333333)} range:[attribute.string rangeOfString:attribute.string]];
}else{
[attribute addAttributes:@{NSForegroundColorAttributeName:self.isSelf ? UIColorFromRGB(0x333333) : [UIColor whiteColor]} range:[attribute.string rangeOfString:attribute.string]];
}
self.textAttribute = attribute;
YYTextContainer *container = [YYTextContainer containerWithSize:dstRect];
container.maximumNumberOfRows = 0;
YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:attribute];
CGFloat rowHeight = layout.textBoundingSize.height + kGetScaleWidth(25);
self.imageHeight = rowHeight;
self.height = (self.isSelf ? kGetScaleWidth(35) : kGetScaleWidth(65)) + rowHeight;
self.isHiddenAvatar = YES;
}
return self;
}
- (NSMutableAttributedString *)createUrlImageAttribute:(NSString *)imageUrl size:(CGSize)size {
NetImageConfig *config = [[NetImageConfig alloc]init];
///先这样吧
config.autoLoad = YES;
NetImageView *imageView = [[NetImageView alloc]initWithUrl:imageUrl config:config];
imageView.bounds = CGRectMake(0, 0, size.width, size.height);
imageView.layer.masksToBounds = YES;
imageView.contentMode = UIViewContentModeScaleAspectFit;
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;
}
/// 占位的富文本
/// @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;
}
/// 生成一个富文本
/// @param text 富文本的文字
/// @param color 文字的颜色
/// @param font 文字的大小
- (NSMutableAttributedString *)createTextAttribute:(NSString *)text color:(UIColor *)color font:(UIFont *)font {
if (text == nil || text.length <= 0) {
text = @"";
}
NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:text attributes:nil];
attribute.yy_font = 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;
}
- (NSString *)cellContent:(MessageBaseModel *)model {
return @"MessageContentHeadLinesText";
}
@end