194 lines
6.3 KiB
Objective-C
194 lines
6.3 KiB
Objective-C
//
|
||
// MessageCell.m
|
||
// xplan-ios
|
||
//
|
||
// Created by zu on 2021/11/28.
|
||
//
|
||
|
||
#import "MessageCell.h"
|
||
#import "NetImageView.h"
|
||
#import "ThemeColor.h"
|
||
|
||
#import "XPMacro.h"
|
||
#import <Masonry/Masonry.h>
|
||
|
||
@interface MessageCell()
|
||
|
||
/**
|
||
左侧头像(私聊对象)
|
||
*/
|
||
@property (nonatomic, strong) NetImageView * leftAvatar;
|
||
|
||
/**
|
||
右侧头像(自己)
|
||
*/
|
||
@property (nonatomic, strong) NetImageView * rightAvatar;
|
||
|
||
/**
|
||
消息背景
|
||
*/
|
||
@property (nonatomic, strong) UIView * messageBackground;
|
||
@property (nonatomic, strong) MASConstraint * messageBackgroundLeft;
|
||
@property (nonatomic, strong) MASConstraint * messageBackgroundRight;
|
||
|
||
/**
|
||
消息文本
|
||
*/
|
||
@property (nonatomic, strong) UILabel * messageText;
|
||
|
||
@end
|
||
|
||
@implementation MessageCell
|
||
|
||
/**
|
||
当前仅支持 Text Message。
|
||
|
||
扩展方向:
|
||
新增Measure(Layout)工具类,按照 NIMMessageType 进行 measure 和生成对应的 Message content view。
|
||
*/
|
||
+ (CGFloat)measureHeight:(NIMMessage *)message {
|
||
CGFloat minHeight = 45 + 15 * 2;
|
||
if (!message) {
|
||
return minHeight;
|
||
}
|
||
|
||
NSString * messageText = message.text;
|
||
if (!messageText || (message.messageType != NIMMessageTypeTip && message.messageType != NIMMessageTypeText)) {
|
||
messageText = @"未知消息类型";
|
||
}
|
||
|
||
CGSize dstRect = CGSizeMake(KScreenWidth - 15 * 2 * 2 - 45 * 2 - 10 * 2, MAXFLOAT);
|
||
|
||
CGFloat msgHeight = [messageText boundingRectWithSize:dstRect options:NSStringDrawingUsesLineFragmentOrigin attributes:[self messageTextAttibutes] context:nil].size.height;
|
||
CGFloat height = msgHeight + 5 + 15 * 2;
|
||
|
||
return MAX(minHeight, height);
|
||
}
|
||
|
||
+ (NSDictionary<NSAttributedStringKey, id> *)messageTextAttibutes {
|
||
UIFont *font = [UIFont systemFontOfSize:13.f];
|
||
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
|
||
[paragraphStyle setLineSpacing:2.5];
|
||
return @{
|
||
NSFontAttributeName:font,
|
||
NSParagraphStyleAttributeName: paragraphStyle
|
||
};
|
||
}
|
||
|
||
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
|
||
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
|
||
if (self) {
|
||
self.selectionStyle = UITableViewCellSelectionStyleNone;
|
||
[self initViews];
|
||
[self initLayout];
|
||
}
|
||
return self;
|
||
}
|
||
|
||
- (void)initViews {
|
||
self.backgroundColor = UIColor.clearColor;
|
||
self.leftAvatar.backgroundColor = UIColor.redColor;
|
||
self.rightAvatar.backgroundColor = UIColor.blueColor;
|
||
[self.contentView addSubview:self.leftAvatar];
|
||
[self.contentView addSubview:self.rightAvatar];
|
||
[self.messageBackground addSubview:self.messageText];
|
||
[self.contentView addSubview:self.messageBackground];
|
||
}
|
||
|
||
- (void)initLayout {
|
||
[self.leftAvatar mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.left.top.mas_equalTo(self).offset(15);
|
||
make.width.height.mas_equalTo(45);
|
||
}];
|
||
|
||
[self.rightAvatar mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.top.mas_equalTo(self).offset(15);
|
||
make.right.mas_equalTo(self).offset(-15);
|
||
make.width.height.mas_equalTo(45);
|
||
}];
|
||
|
||
[self.messageBackground mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
self.messageBackgroundLeft = make.left.mas_equalTo(self.leftAvatar.mas_right).offset(15);
|
||
self.messageBackgroundRight = make.right.mas_equalTo(self.rightAvatar.mas_left).offset(-15);
|
||
make.top.mas_equalTo(self).offset(20);
|
||
}];
|
||
|
||
[self.messageText mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.edges.equalTo(self.messageBackground).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
|
||
}];
|
||
}
|
||
|
||
- (void)renderWithMessage:(NIMMessage *)message {
|
||
NSString * avatarUrl = [[NIMSDK sharedSDK].userManager userInfo:message.from].userInfo.avatarUrl;
|
||
avatarUrl = [avatarUrl stringByReplacingOccurrencesOfString:@"https" withString:@"http"];
|
||
BOOL isSelf = [[NIMSDK sharedSDK].loginManager.currentAccount isEqualToString:message.from];
|
||
if (isSelf) {
|
||
self.leftAvatar.hidden = YES;
|
||
self.rightAvatar.hidden = NO;
|
||
[self.messageBackgroundLeft uninstall];
|
||
[self.messageBackgroundRight install];
|
||
self.rightAvatar.imageUrl = avatarUrl;
|
||
} else {
|
||
self.leftAvatar.hidden = NO;
|
||
self.rightAvatar.hidden = YES;
|
||
[self.messageBackgroundLeft install];
|
||
[self.messageBackgroundRight uninstall];
|
||
self.leftAvatar.imageUrl = avatarUrl;
|
||
}
|
||
|
||
NSString * messageText = message.text;
|
||
if (!messageText || (message.messageType != NIMMessageTypeTip && message.messageType != NIMMessageTypeText)) {
|
||
messageText = @"未知消息类型";
|
||
}
|
||
|
||
_messageText.attributedText = [[NSAttributedString alloc] initWithString:messageText attributes:[MessageCell messageTextAttibutes]];
|
||
}
|
||
|
||
- (NetImageView *)leftAvatar {
|
||
if (!_leftAvatar) {
|
||
NetImageConfig * config = [[NetImageConfig alloc] init];
|
||
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
|
||
config.radius = MAXFLOAT;
|
||
_leftAvatar = [[NetImageView alloc] initWithConfig:config];
|
||
_leftAvatar.layer.masksToBounds = YES;
|
||
_leftAvatar.layer.cornerRadius = 45.f / 2;
|
||
_leftAvatar.hidden = YES;
|
||
}
|
||
return _leftAvatar;
|
||
}
|
||
|
||
- (NetImageView *)rightAvatar {
|
||
if (!_rightAvatar) {
|
||
NetImageConfig * config = [[NetImageConfig alloc] init];
|
||
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
|
||
config.radius = MAXFLOAT;
|
||
_rightAvatar = [[NetImageView alloc] initWithConfig:config];
|
||
_rightAvatar.layer.masksToBounds = YES;
|
||
_rightAvatar.layer.cornerRadius = 45.f / 2;
|
||
_rightAvatar.hidden = YES;
|
||
}
|
||
return _rightAvatar;
|
||
}
|
||
|
||
- (UIView *)messageBackground {
|
||
if (!_messageBackground) {
|
||
_messageBackground = [[UIView alloc]init];
|
||
_messageBackground.backgroundColor = ThemeColor.appCellBackgroundColor;
|
||
_messageBackground.layer.masksToBounds = YES;
|
||
_messageBackground.layer.cornerRadius = 8.f;
|
||
}
|
||
return _messageBackground;
|
||
}
|
||
|
||
- (UILabel *)messageText {
|
||
if (!_messageText) {
|
||
_messageText = [[UILabel alloc]initWithFrame:CGRectZero];
|
||
_messageText.preferredMaxLayoutWidth = KScreenWidth - 15 * 2 * 2 - 45 * 2 - 10 * 2;
|
||
_messageText.textColor = ThemeColor.mainTextColor;
|
||
_messageText.numberOfLines = 0;
|
||
}
|
||
return _messageText;
|
||
}
|
||
|
||
@end
|