更新 XPRoomMessageParser.m,优化房间表情属性创建逻辑,简化代码结构;新增 createImageAttribute:size: 方法以支持自定义图像大小,增强代码可读性和灵活性;调整图像处理逻辑,确保在不同条件下正确展示表情图像,保持代码一致性。
This commit is contained in:
@@ -1040,32 +1040,57 @@
|
||||
|
||||
if ([attachment.data[@"data"] isKindOfClass:[NSArray class]]) {
|
||||
NSArray * array = [RoomFaceSendInfoModel modelsWithArray:attachment.data[@"data"]];
|
||||
for (int i = 0; i< array.count; i++) {
|
||||
RoomFaceSendInfoModel * sendFaceInfo = [array xpSafeObjectAtIndex:i];
|
||||
if (array.count > 0) {
|
||||
RoomFaceSendInfoModel * sendFaceInfo = [array xpSafeObjectAtIndex:0];
|
||||
[attribute appendAttributedString:[self createTextAttribute:sendFaceInfo.nick color:[DJDKMIMOMColor messageDefaultTextColor] font:kRoomMessageDefalutFont]];
|
||||
[attribute appendAttributedString:[self createTextAttribute:@":" color:[DJDKMIMOMColor messageDefaultTextColor] font:kRoomMessageDefalutFont]];
|
||||
[attribute appendAttributedString:[self createTextAttribute:@"\n" color:[DJDKMIMOMColor messageDefaultTextColor] font:kRoomMessageDefalutFont]];
|
||||
|
||||
#if DEBUG
|
||||
sendFaceInfo.faceId = 1;
|
||||
NSInteger originalID = sendFaceInfo.faceId;
|
||||
if (sendFaceInfo.faceId == 42) {
|
||||
sendFaceInfo.faceId = 1;
|
||||
}
|
||||
#endif
|
||||
if (sendFaceInfo.faceId == 1) {
|
||||
UIImage *image = kImage(@"幸运数字");
|
||||
[attribute appendAttributedString:[self createImageAttribute:[image cutImage:CGSizeMake(30, 30)]]];
|
||||
[attribute appendAttributedString:[self createImageAttribute:image size:CGSizeMake(40, 40)]];
|
||||
}
|
||||
|
||||
[attribute appendAttributedString:[self createTextAttribute:sendFaceInfo.nick color:[DJDKMIMOMColor messageDefaultTextColor] font:kRoomMessageDefalutFont]];
|
||||
[attribute appendAttributedString:[self createTextAttribute:@":" color:[DJDKMIMOMColor messageDefaultTextColor] font:kRoomMessageDefalutFont]];
|
||||
[attribute appendAttributedString:[self createTextAttribute:@"\n" color:[DJDKMIMOMColor messageDefaultTextColor] font:kRoomMessageDefalutFont]];
|
||||
for (int j = 0; j < sendFaceInfo.resultIndexes.count; j++) {
|
||||
NSNumber *index = sendFaceInfo.resultIndexes[j];
|
||||
UIImage *face = [[XPRoomFaceTool shareFaceTool] findFaceImageById:sendFaceInfo.faceId index:[index integerValue]];
|
||||
if (face) {
|
||||
UIImageView *imageView = [[UIImageView alloc]init];
|
||||
imageView.image = face;
|
||||
imageView.contentMode = UIViewContentModeScaleAspectFit;
|
||||
imageView.bounds = CGRectMake(0, 0, 30, 30);
|
||||
NSMutableAttributedString * attrString = [NSMutableAttributedString yy_attachmentStringWithContent:imageView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height) alignToFont:kFontRegular(kRoomMessageDefalutFont) alignment:YYTextVerticalAlignmentCenter];
|
||||
[attribute appendAttributedString:[self createSapceAttribute:1]];
|
||||
[attribute appendAttributedString:attrString];
|
||||
}
|
||||
}
|
||||
#if DEBUG
|
||||
sendFaceInfo.faceId = originalID;
|
||||
#endif
|
||||
}
|
||||
|
||||
for (int i = 0; i< array.count; i++) {
|
||||
RoomFaceSendInfoModel * sendFaceInfo = [array xpSafeObjectAtIndex:i];
|
||||
if (sendFaceInfo.resultIndexes.count == 0) {
|
||||
RoomFaceInfoModel *configInfo = [[XPRoomFaceTool shareFaceTool] findFaceInfoById:sendFaceInfo.faceId];
|
||||
UIImage *face = [[XPRoomFaceTool shareFaceTool] findFaceImageById:sendFaceInfo.faceId index:configInfo.animStartPos];
|
||||
if (face) {
|
||||
UIImageView *imageView = [[UIImageView alloc]init];
|
||||
imageView.image = face;
|
||||
imageView.contentMode = UIViewContentModeScaleAspectFit;
|
||||
imageView.bounds = CGRectMake(0, 0, 40, 40);
|
||||
NSMutableAttributedString * attrString = [NSMutableAttributedString yy_attachmentStringWithContent:imageView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height) alignToFont:kFontRegular(kRoomMessageDefalutFont) alignment:YYTextVerticalAlignmentCenter];
|
||||
[attribute appendAttributedString:[self createSapceAttribute:1]];
|
||||
[attribute appendAttributedString:attrString];
|
||||
}
|
||||
} else {
|
||||
for (int j = 0; j < sendFaceInfo.resultIndexes.count; j++) {
|
||||
NSNumber *index = sendFaceInfo.resultIndexes[j];
|
||||
UIImage *face = [[XPRoomFaceTool shareFaceTool] findFaceImageById:sendFaceInfo.faceId index:[index integerValue]];
|
||||
if (face) {
|
||||
UIImageView *imageView = [[UIImageView alloc]init];
|
||||
imageView.image = face;
|
||||
imageView.contentMode = UIViewContentModeScaleAspectFit;
|
||||
imageView.bounds = CGRectMake(0, 0, 30, 30);
|
||||
NSMutableAttributedString * attrString = [NSMutableAttributedString yy_attachmentStringWithContent:imageView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height) alignToFont:kFontRegular(kRoomMessageDefalutFont) alignment:YYTextVerticalAlignmentCenter];
|
||||
[attribute appendAttributedString:[self createSapceAttribute:1]];
|
||||
[attribute appendAttributedString:attrString];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
messageInfo.content = attribute;
|
||||
@@ -1781,6 +1806,18 @@
|
||||
return attrString;
|
||||
}
|
||||
|
||||
- (NSMutableAttributedString *)createImageAttribute:(UIImage *)image size:(CGSize)size{
|
||||
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,
|
||||
size.width,
|
||||
size.height);
|
||||
NSMutableAttributedString * attrString = [NSMutableAttributedString yy_attachmentStringWithContent:imaveView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(imaveView.frame.size.width, imaveView.frame.size.height) alignToFont:kFontRegular(kRoomMessageDefalutFont) alignment:YYTextVerticalAlignmentCenter];
|
||||
return attrString;
|
||||
}
|
||||
|
||||
/// 生成一个富文本
|
||||
/// @param text 富文本的文字
|
||||
/// @param color 文字的颜色
|
||||
|
Reference in New Issue
Block a user