82 lines
2.4 KiB
Objective-C
82 lines
2.4 KiB
Objective-C
//
|
|
// MessageContentText.m
|
|
// xplan-ios
|
|
//
|
|
// Created by zu on 2021/12/2.
|
|
//
|
|
|
|
#import "MessageContentText.h"
|
|
#import "ThemeColor.h"
|
|
#import "NSObject+MJExtension.h"
|
|
|
|
#import <NIMSDK/NIMSDK.h>
|
|
#import <Masonry/Masonry.h>
|
|
|
|
#define MESSAGE_TEXT_PADDING 10
|
|
|
|
@interface MessageContentText()
|
|
|
|
/**
|
|
消息文本
|
|
*/
|
|
@property (nonatomic, strong) UILabel * messageText;
|
|
|
|
@end
|
|
|
|
@implementation MessageContentText
|
|
|
|
+ (CGFloat)measureHeight:(NIMMessage *)message {
|
|
NSString * messageText = message.text;
|
|
if (!messageText) {
|
|
messageText = @"未知消息类型";
|
|
}
|
|
|
|
CGSize dstRect = CGSizeMake(CONTENT_WIDTH_MAX - MESSAGE_TEXT_PADDING * 2, MAXFLOAT);
|
|
|
|
CGFloat msgHeight = [messageText boundingRectWithSize:dstRect options:NSStringDrawingUsesLineFragmentOrigin attributes:[MessageContentText messageTextAttibutes] context:nil].size.height;
|
|
|
|
return msgHeight + MESSAGE_TEXT_PADDING * 2 + CONTENT_PADDING_V_TOTAL;
|
|
}
|
|
|
|
+ (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)init {
|
|
self = [super init];
|
|
if (self) {
|
|
[self addSubview:self.messageText];
|
|
[self.messageText mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.edges.equalTo(self).with.insets(UIEdgeInsetsMake(MESSAGE_TEXT_PADDING, MESSAGE_TEXT_PADDING, MESSAGE_TEXT_PADDING, MESSAGE_TEXT_PADDING));
|
|
}];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)render:(nonnull NIMMessage *)message {
|
|
NSString * messageText = message.text;
|
|
if (!messageText || (message.messageType != NIMMessageTypeTip && message.messageType != NIMMessageTypeText)) {
|
|
messageText = @"未知消息类型";
|
|
}
|
|
NSDictionary * dic = message.yidunAntiSpamRes.toJSONObject;
|
|
_messageText.attributedText = [[NSAttributedString alloc] initWithString:messageText attributes:[MessageContentText messageTextAttibutes]];
|
|
}
|
|
|
|
- (UILabel *)messageText {
|
|
if (!_messageText) {
|
|
_messageText = [[UILabel alloc]initWithFrame:CGRectZero];
|
|
_messageText.preferredMaxLayoutWidth = CONTENT_WIDTH_MAX - MESSAGE_TEXT_PADDING * 2;
|
|
_messageText.textColor = ThemeColor.mainTextColor;
|
|
_messageText.numberOfLines = 0;
|
|
}
|
|
return _messageText;
|
|
}
|
|
|
|
@end
|