Files
yinmeng-ios/xplan-ios/Main/Message/View/Session/Content/MessageContentTextClickable.m
2022-05-13 16:12:18 +08:00

211 lines
7.5 KiB
Objective-C

//
// MessageContentTextClickable.m
// xplan-ios
//
// Created by zu on 2022/1/24.
//
#import "MessageContentTextClickable.h"
///Third
#import <Masonry/Masonry.h>
#import <NIMSDK/NIMSDK.h>
///Tool
#import "ThemeColor.h"
#import "NSObject+MJExtension.h"
#import "XCCurrentVCStackManager.h"
///Model
#import "AttachmentModel.h"
#import "ContentSecretaryModel.h"
///View
#import "XPRoomViewController.h"
#import "XPWebViewController.h"
#import "XPMineRechargeViewController.h"
#import "XPMineUserInfoViewController.h"
#import "XPMineDressUpViewController.h"
#import "XPNobleCenterViewController.h"
///P
#import "RoomHostDelegate.h"
@interface MessageContentTextClickable()
/**
消息文本
*/
@property (nonatomic, strong) UILabel * messageText;
///获取的模型
@property (nonatomic,strong) ContentSecretaryModel *contentInfo;
@end
@implementation MessageContentTextClickable
+ (CGFloat)measureHeight:(NIMMessage *)message {
NSString * messageText = message.text;
if (message.messageType == NIMMessageTypeCustom) {
NIMCustomObject *obj = (NIMCustomObject *) message.messageObject;
AttachmentModel *attachment = (AttachmentModel *) obj.attachment;
if (attachment.first == CustomMessageType_Secretary) {
if (attachment.second == Custom_Message_Sub_Secretary_Router) {
messageText = attachment.data[@"msg"];
}
}
}
if (!messageText) {
messageText = @"未知消息类型";
}
CGSize dstRect = CGSizeMake(CONTENT_WIDTH_MAX - MESSAGE_PADDING * 2, MAXFLOAT);
CGFloat msgHeight = [messageText boundingRectWithSize:dstRect options:NSStringDrawingUsesLineFragmentOrigin attributes:[MessageContentTextClickable messageTextAttibutes] context:nil].size.height;
return msgHeight + MESSAGE_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
};
}
- (void)initSubViews {
[super initSubViews];
[self.backView addSubview:self.messageText];
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(event:)];
[self addGestureRecognizer:tapGesture];
}
- (void)initSubViewConstraints {
[super initSubViewConstraints];
[self.messageText mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.backView).insets(UIEdgeInsetsMake(0, 0, 0, 0));
}];
}
- (void)render:(nonnull NIMMessage *)message {
NSString * messageText = message.text;
if (message.messageType == NIMMessageTypeCustom) {
NIMCustomObject *obj = (NIMCustomObject *) message.messageObject;
AttachmentModel *attachment = (AttachmentModel *) obj.attachment;
if (attachment.first == CustomMessageType_Secretary) {
if (attachment.second == Custom_Message_Sub_Secretary_Router) {
self.contentInfo = [ContentSecretaryModel modelWithJSON:attachment.data];
messageText = self.contentInfo.msg;
}
}
}
if (!messageText) {
messageText = @"未知消息类型";
}
_messageText.attributedText = [[NSAttributedString alloc] initWithString:messageText attributes:[MessageContentTextClickable messageTextAttibutes]];
}
- (void)event:(UITapGestureRecognizer *)gesture {
SecretaryRouterType type = self.contentInfo.routerType;
NSString * value = self.contentInfo.routerValue;
switch (type) {
case SecretaryRouterType_H5: {
if (value.length) {
XPWebViewController * webVC =[[XPWebViewController alloc] init];
webVC.url = value;
[self.nim_viewController.navigationController pushViewController:webVC animated:YES];
}
}
break;
case SecretaryRouterType_Room:
{
if (value.length > 0) {
UIViewController * controllerView = [XCCurrentVCStackManager shareManager].getCurrentVC;
//退出原来的房间 如果有的话 TODO 总感觉这种处理不太优雅 进房入口多了 怎么办 进房需要整合
[controllerView.navigationController.viewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:[XPRoomViewController class]]) {
[controllerView.navigationController popToRootViewControllerAnimated:NO];
XPRoomViewController<RoomHostDelegate> * rooomVC = obj;
[rooomVC exitRoom];
*stop = YES;
}
}];
[XPRoomViewController openRoom:value viewController:[XCCurrentVCStackManager shareManager].getCurrentVC];
}
}
break;
case SecretaryRouterType_Purse:
case SecretaryRouterType_Recharge:
{
XPMineRechargeViewController * rechargeVC = [[XPMineRechargeViewController alloc] init];
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController pushViewController:rechargeVC animated:YES];
}
break;
case SecretaryRouterType_UserInfo:
{
if (value.length > 0) {
XPMineUserInfoViewController * userInfoVC = [[XPMineUserInfoViewController alloc] init];
userInfoVC.uid = value.integerValue;
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController pushViewController:userInfoVC animated:YES];
}
}
case SecretaryRouterType_Car:
{
XPMineDressUpViewController * dressUpVC = [[XPMineDressUpViewController alloc] init];
dressUpVC.currentIndex = 1;
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController pushViewController:dressUpVC animated:YES];
}
break;
case SecretaryRouterType_Headwear:
{
XPMineDressUpViewController * dressUpVC = [[XPMineDressUpViewController alloc] init];
dressUpVC.currentIndex = 0;
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController pushViewController:dressUpVC animated:YES];
}
break;
case SecretaryRouterType_Nameplate:
{
XPMineDressUpViewController * dressUpVC = [[XPMineDressUpViewController alloc] init];
dressUpVC.currentIndex = 2;
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController pushViewController:dressUpVC animated:YES];
}
break;
case SecretaryRouterType_Nobel_Bubble:
{
XPMineDressUpViewController * dressUpVC = [[XPMineDressUpViewController alloc] init];
dressUpVC.currentIndex = 4;
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController pushViewController:dressUpVC animated:YES];
}
break;
case SecretaryRouterType_User_Card:
{
XPMineDressUpViewController * dressUpVC = [[XPMineDressUpViewController alloc] init];
dressUpVC.currentIndex = 3;
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController pushViewController:dressUpVC animated:YES];
}
break;
case SecretaryRouterType_Nobel_Center:
{
XPNobleCenterViewController * nobelCenterVC = [[XPNobleCenterViewController alloc] init];
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController pushViewController:nobelCenterVC animated:YES];
}
break;
case SecretaryRouterType_Set_Password:
{
}
break;
default:
break;
}
}
- (UILabel *)messageText {
if (!_messageText) {
_messageText = [[UILabel alloc]initWithFrame:CGRectZero];
_messageText.preferredMaxLayoutWidth = CONTENT_WIDTH_MAX - MESSAGE_PADDING * 2;
_messageText.textColor = ThemeColor.mainTextColor;
_messageText.numberOfLines = 0;
}
return _messageText;
}
@end