Files
yinmeng-ios/xplan-ios/Main/Message/View/Session/Content/MessageContentTweetView.m
2024-01-30 16:46:31 +08:00

264 lines
10 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// MessageContentTweetView.m
// xplan-ios
//
// Created by 冯硕 on 2022/4/28.
//
#import "MessageContentTweetView.h"
///Third
#import <Masonry/Masonry.h>
///Tool
#import "ThemeColor.h"
#import "NetImageView.h"
#import "NSObject+MJExtension.h"
#import "XCCurrentVCStackManager.h"
#import "XPConstant.h"
///Model
#import "ContentTweetModel.h"
///View
#import "XPWebViewController.h"
#import "XPRoomViewController.h"
@interface MessageContentTweetView ()
///显示图片
@property (nonatomic,strong) NetImageView *logoImageView;
///显示名字
@property (nonatomic,strong) UILabel *titleLabel;
///分割线
@property (nonatomic,strong) UIView * lineView;
///显示副标题
@property (nonatomic,strong) UILabel *subTitleLabel;
///查看详情
@property (nonatomic,strong) UIButton *checkButton;
///是否为新消息
@property (nonatomic, strong) UIImageView *isNewImageView;
///新闻的模型
@property (nonatomic,strong) ContentTweetModel *tweetModel;
///消息ID用于记录是否已读
@property (nonatomic, copy) NSString *messageId;
@end
@implementation MessageContentTweetView
+ (CGFloat)measureHeight:(NIMMessage *)message {
NIMCustomObject *obj = (NIMCustomObject *)message.messageObject;
AttachmentModel * attach = obj.attachment;
ContentTweetModel * model = [ContentTweetModel modelWithJSON:attach.data];
CGFloat oneHeight = [@"这个是计算一行的高度的" boundingRectWithSize:CGSizeMake(240, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin
attributes:[self messageTextAttibutes:[UIFont systemFontOfSize:16]] context:nil].size.height + 2.5;
CGFloat titleHeight = [model.title boundingRectWithSize:CGSizeMake(240, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin
attributes:[self messageTextAttibutes:[UIFont systemFontOfSize:16]] context:nil].size.height;
if (titleHeight <= oneHeight * 2) {
titleHeight = titleHeight + 5;
} else {
titleHeight = oneHeight * 2;
}
CGFloat desHeight = [model.desc boundingRectWithSize:CGSizeMake(240, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin
attributes:[self messageTextAttibutes:[UIFont systemFontOfSize:13]] context:nil].size.height;
return titleHeight + desHeight + 85 + 20 + 1 + 35 + MESSAGE_PADDING * 2 + CONTENT_PADDING_V_TOTAL;
}
- (void)render:(NIMMessage *)message {
NIMCustomObject *obj = (NIMCustomObject *)message.messageObject;
AttachmentModel * attach = obj.attachment;
ContentTweetModel * model = [ContentTweetModel modelWithJSON:attach.data];
self.tweetModel = model;
self.messageId = message.messageId;
self.logoImageView.imageUrl = model.picUrl;
self.titleLabel.attributedText = [[NSMutableAttributedString alloc] initWithString:model.title attributes:[self messageTextAttibutes:[UIFont systemFontOfSize:16] color:[ThemeColor mainTextColor]]];
self.subTitleLabel.attributedText = [[NSMutableAttributedString alloc] initWithString:model.desc attributes:[self messageTextAttibutes:[UIFont systemFontOfSize:13] color:[ThemeColor secondTextColor]]];
CGFloat oneHeight = [@"这个是计算一行的高度的" boundingRectWithSize:CGSizeMake(240, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin
attributes:[self messageTextAttibutes:[UIFont systemFontOfSize:16] color:[ThemeColor mainTextColor]] context:nil].size.height + 2.5;
CGFloat titleHeight = [model.title boundingRectWithSize:CGSizeMake(240, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin
attributes:[self messageTextAttibutes:[UIFont systemFontOfSize:16] color:[ThemeColor mainTextColor]] context:nil].size.height;
if (titleHeight <= oneHeight * 2) {
[self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(titleHeight + 5);
}];
} else{
[self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(oneHeight * 2);
}];
}
CGFloat desHeight = [model.desc boundingRectWithSize:CGSizeMake(240, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin
attributes:[self messageTextAttibutes:[UIFont systemFontOfSize:13] color:[ThemeColor mainTextColor]] context:nil].size.height + 10;
[self.subTitleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(desHeight);
}];
NSArray *redPacketHistory = [[NSUserDefaults standardUserDefaults] objectForKey:kTuWenMessageHistory];
if ([redPacketHistory containsObject: message.messageId]) {
self.isNewImageView.hidden = YES;
} else {
self.isNewImageView.hidden = NO;
}
}
- (NSDictionary<NSAttributedStringKey, id> *)messageTextAttibutes:(UIFont *)font color:(UIColor *)color {
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:2.5];
return @{
NSFontAttributeName:font,
NSParagraphStyleAttributeName: paragraphStyle,
NSForegroundColorAttributeName:color
};
}
+ (NSDictionary<NSAttributedStringKey, id> *)messageTextAttibutes:(UIFont *)font {
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:2.5];
return @{
NSFontAttributeName:font,
NSParagraphStyleAttributeName: paragraphStyle
};
}
- (void)initSubViews {
[super initSubViews];
[self.backView addSubview:self.titleLabel];
[self.backView addSubview:self.logoImageView];
[self.backView addSubview:self.lineView];
[self.backView addSubview:self.subTitleLabel];
[self.backView addSubview:self.checkButton];
[self.backView addSubview:self.isNewImageView];
}
- (void)initSubViewConstraints {
[super initSubViewConstraints];
[self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(280);
make.bottom.mas_equalTo(self.checkButton);
}];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.backView);
make.top.mas_equalTo(self.backView);
make.height.mas_equalTo(18);
}];
[self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.backView);
make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(10);
make.height.mas_equalTo(87);
}];
[self.subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.backView);
make.top.mas_equalTo(self.logoImageView.mas_bottom).offset(10);
make.height.mas_equalTo(18);
}];
[self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.backView);
make.height.mas_equalTo(0.5);
make.top.mas_equalTo(self.subTitleLabel.mas_bottom).offset(5);
}];
[self.checkButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.backView);
make.top.mas_equalTo(self.lineView.mas_bottom).offset(10);
make.height.mas_equalTo(20);
}];
[self.isNewImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(self.backView);
make.top.mas_equalTo(self.backView).mas_offset(3);
make.size.mas_equalTo(CGSizeMake(24, 10));
}];
}
#pragma mark - Event Response
- (void)checkButtonAction:(UIButton *)sender {
// 如果打开红包成功 则记录当前红包状态
NSArray *history = [[NSUserDefaults standardUserDefaults] objectForKey:kTuWenMessageHistory];
NSMutableArray *tempArray = [NSMutableArray arrayWithArray:history];
if (![tempArray containsObject:self.messageId]) {
[tempArray addObject:self.messageId];
[[NSUserDefaults standardUserDefaults] setObject:tempArray forKey:kTuWenMessageHistory];
[[NSUserDefaults standardUserDefaults] synchronize];
}
if (self.tweetModel.routerType == 1) {
[XPRoomViewController openRoom:self.tweetModel.routerValue viewController:[XCCurrentVCStackManager shareManager].getCurrentVC];
} else if (self.tweetModel.routerType == 2) {
XPWebViewController * webVC = [[XPWebViewController alloc] init];
webVC.url = self.tweetModel.routerValue;
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController pushViewController:webVC animated:YES];
} else {
if ([self.tweetModel.webUrl hasPrefix:@"http"]) {
XPWebViewController * webVC = [[XPWebViewController alloc] init];
webVC.url = self.tweetModel.webUrl;
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController pushViewController:webVC animated:YES];
}
}
}
#pragma mark - Getters And Setters
- (NetImageView *)logoImageView {
if (!_logoImageView) {
NetImageConfig * config = [[NetImageConfig alloc]init];
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
_logoImageView = [[NetImageView alloc] initWithConfig:config];
_logoImageView.layer.masksToBounds = YES;
_logoImageView.layer.cornerRadius = 10;
_logoImageView.contentMode = UIViewContentModeScaleAspectFill;
_logoImageView.layer.masksToBounds = YES;
}
return _logoImageView;
}
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.numberOfLines = 2;
_titleLabel.preferredMaxLayoutWidth = 240;
}
return _titleLabel;
}
- (UIView *)lineView {
if (!_lineView) {
_lineView = [[UIView alloc] init];
_lineView.backgroundColor = [ThemeColor dividerColor];
}
return _lineView;
}
- (UILabel *)subTitleLabel {
if (!_subTitleLabel) {
_subTitleLabel = [[UILabel alloc] init];
_subTitleLabel.numberOfLines = 0;
_subTitleLabel.preferredMaxLayoutWidth = 240;
}
return _subTitleLabel;
}
- (UIButton *)checkButton {
if (!_checkButton) {
_checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_checkButton setTitle:@"立即查看" forState:UIControlStateNormal];
[_checkButton setTitleColor:UIColorFromRGB(0x4C5AF1) forState:UIControlStateNormal];
_checkButton.titleLabel.font = [UIFont systemFontOfSize:14];
[_checkButton addTarget:self action:@selector(checkButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _checkButton;
}
- (UIImageView *)isNewImageView {
if (!_isNewImageView) {
_isNewImageView = [[UIImageView alloc] init];
_isNewImageView.image = [UIImage imageNamed:@"gift_type_newIcon"];
_isNewImageView.hidden = YES;
}
return _isNewImageView;
}
@end