Files
yinmeng-ios/xplan-ios/Main/Monents/View/Cell/XPMonentsInteractiveTableViewCell.m
2022-08-26 21:38:47 +08:00

309 lines
10 KiB
Objective-C

//
// XPMonentsInteractiveTableViewCell.m
// xplan-ios
//
// Created by 冯硕 on 2022/5/19.
//
#import "XPMonentsInteractiveTableViewCell.h"
///Third
#import <Masonry/Masonry.h>
///Tool
#import "ThemeColor.h"
#import "NetImageView.h"
#import "XPMacro.h"
#import "UIImage+Utils.h"
///Model
#import "MonentsInteractiveModel.h"
@interface XPMonentsInteractiveTableViewCell ()
///容器
@property (nonatomic,strong) UIStackView *stackView;
///显示话题
@property (nonatomic,strong) UIButton *topicButton;
///显示内容
@property (nonatomic,strong) UIView * infoView;
///显示头像
@property (nonatomic,strong) NetImageView *avatarImageView;
///昵称
@property (nonatomic,strong) UILabel *nickLabel;
///性别
@property (nonatomic,strong) UIImageView *sexImageView;
///评论了你
@property (nonatomic,strong) UILabel *commentLabel;
///赞了动态或者显示评论的内容
@property (nonatomic,strong) UILabel *contentLabel;
///时间
@property (nonatomic,strong) UILabel *timeLabel;
///动态的图片
@property (nonatomic,strong) NetImageView *dynamicImageView;
///动态的内容
@property (nonatomic,strong) UILabel *dynamicLabel;
///显示分割线
@property (nonatomic,strong) UIView * lineView;
///时间格式
@property (nonatomic,strong) NSDateFormatter *dataFormatter;
@end
@implementation XPMonentsInteractiveTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
self.backgroundColor = [ThemeColor appCellBackgroundColor];
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self.contentView addSubview:self.stackView];
[self.stackView addArrangedSubview:self.topicButton];
[self.stackView addArrangedSubview:self.infoView];
[self.infoView addSubview:self.avatarImageView];
[self.infoView addSubview:self.nickLabel];
[self.infoView addSubview:self.sexImageView];
[self.infoView addSubview:self.commentLabel];
[self.infoView addSubview:self.contentLabel];
[self.infoView addSubview:self.timeLabel];
[self.infoView addSubview:self.dynamicImageView];
[self.infoView addSubview:self.dynamicLabel];
[self.infoView addSubview:self.lineView];
}
- (void)initSubViewConstraints {
[self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.contentView).inset(15);
make.top.mas_equalTo(self.contentView).offset(10);
make.bottom.mas_equalTo(self.contentView);
}];
[self.topicButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(30);
make.width.mas_equalTo(50);
}];
[self.infoView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(KScreenWidth- 15 * 2);
}];
[self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(44, 44));
make.left.top.mas_equalTo(self.infoView);
}];
[self.nickLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.avatarImageView.mas_right).offset(10);
make.top.mas_equalTo(self.avatarImageView);
}];
[self.sexImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(15, 15));
make.centerY.mas_equalTo(self.nickLabel);
make.left.mas_equalTo(self.nickLabel.mas_right).offset(2);
}];
[self.commentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.nickLabel);
make.left.mas_equalTo(self.sexImageView.mas_right).offset(2);
}];
[self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.nickLabel.mas_bottom).offset(10);
make.left.mas_equalTo(self.nickLabel);
make.right.mas_lessThanOrEqualTo(self.dynamicImageView.mas_left).offset(-5);
}];
[self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.nickLabel);
make.top.mas_equalTo(self.contentLabel.mas_bottom).offset(10);
}];
[self.dynamicImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(60, 60));
make.right.mas_equalTo(self.infoView);
make.centerY.mas_equalTo(self.infoView);
}];
[self.dynamicLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.right.mas_equalTo(self.dynamicImageView);
}];
[self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.nickLabel);
make.right.mas_equalTo(self.infoView);
make.height.mas_equalTo(1);
make.bottom.mas_equalTo(self.infoView);
}];
}
#pragma mark - Getters And Setters
- (void)setInteractiveInfo:(MonentsInteractiveModel *)interactiveInfo {
_interactiveInfo = interactiveInfo;
if (_interactiveInfo) {
self.topicButton.hidden = _interactiveInfo.worldName.length <=0;
[self.topicButton setTitle:_interactiveInfo.worldName forState:UIControlStateNormal];
self.avatarImageView.imageUrl = _interactiveInfo.avatar;
NSString * nick = interactiveInfo.nick;
if (nick.length > 6) {
nick = [NSString stringWithFormat:@"%@…", [nick substringToIndex:6]];
}
self.nickLabel.text = nick.length > 0 ? nick : @"";
self.sexImageView.image = _interactiveInfo.gender == GenderType_Female ? [UIImage imageNamed:@"common_female"] : [UIImage imageNamed:@"common_male"];
BOOL isComment = _interactiveInfo.actionType == MonentsInteractiveActionType_Comment;
self.commentLabel.text = isComment ? @"评论了你" : @"";
BOOL isText = _interactiveInfo.type == MonentsContentType_Text;
self.dynamicImageView.hidden = isText;
self.dynamicLabel.hidden = !isText;
if (isText) {
self.dynamicLabel.text = _interactiveInfo.content;
} else {
self.dynamicImageView.imageUrl = _interactiveInfo.dynamicRes.resUrl;
}
self.contentLabel.text = _interactiveInfo.message;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interactiveInfo.publishTime.floatValue/1000];
NSString *dateString = [self.dataFormatter stringFromDate:date];
self.timeLabel.text = dateString;
CGFloat width = [_interactiveInfo.worldName boundingRectWithSize:CGSizeMake(150,CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil].size.width + 10;
[self.topicButton mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(width);
}];
}
}
- (UIStackView *)stackView {
if (!_stackView) {
_stackView = [[UIStackView alloc] init];
_stackView.axis = UILayoutConstraintAxisVertical;
_stackView.distribution = UIStackViewDistributionFill;
_stackView.alignment = UIStackViewAlignmentLeading;
_stackView.spacing = 10;
}
return _stackView;
}
- (UIButton *)topicButton {
if (!_topicButton) {
_topicButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_topicButton setTitle:@"邀请" forState:UIControlStateNormal];
[_topicButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_topicButton.titleLabel.font = [UIFont systemFontOfSize:12];
[_topicButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[[ThemeColor confirmButtonGradientStartColor], [ThemeColor confirmButtonGradientEndColor]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(10, 10)] forState:UIControlStateNormal];
_topicButton.layer.masksToBounds = YES;
_topicButton.layer.cornerRadius = 15;
}
return _topicButton;
}
- (UIView *)infoView {
if (!_infoView) {
_infoView = [[UIView alloc] init];
_infoView.backgroundColor = [UIColor clearColor];
}
return _infoView;
}
- (NetImageView *)avatarImageView {
if (!_avatarImageView) {
NetImageConfig * config = [[NetImageConfig alloc]init];
config.imageType = ImageTypeUserIcon;
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
_avatarImageView = [[NetImageView alloc] initWithConfig:config];
_avatarImageView.layer.masksToBounds = YES;
_avatarImageView.layer.cornerRadius = 22;
}
return _avatarImageView;
}
- (UILabel *)nickLabel {
if (!_nickLabel) {
_nickLabel = [[UILabel alloc] init];
_nickLabel.font = [UIFont systemFontOfSize:15];
_nickLabel.textColor = [ThemeColor textThirdColor];
}
return _nickLabel;
}
- (UIImageView *)sexImageView {
if (!_sexImageView) {
_sexImageView = [[UIImageView alloc] init];
_sexImageView.userInteractionEnabled = YES;
}
return _sexImageView;
}
- (UILabel *)commentLabel {
if (!_commentLabel) {
_commentLabel = [[UILabel alloc] init];
_commentLabel.font = [UIFont systemFontOfSize:15];
_commentLabel.textColor = [ThemeColor textThirdColor];
}
return _commentLabel;
}
- (UILabel *)contentLabel {
if (!_contentLabel) {
_contentLabel = [[UILabel alloc] init];
_contentLabel.font = [UIFont systemFontOfSize:15];
_contentLabel.textColor = [ThemeColor mainTextColor];
}
return _contentLabel;
}
- (UILabel *)timeLabel {
if (!_timeLabel) {
_timeLabel = [[UILabel alloc] init];
_timeLabel.font = [UIFont systemFontOfSize:12];
_timeLabel.textColor = [ThemeColor textThirdColor];
}
return _timeLabel;
}
- (NetImageView *)dynamicImageView {
if (!_dynamicImageView) {
NetImageConfig * config = [[NetImageConfig alloc]init];
config.imageType = ImageTypeUserIcon;
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
_dynamicImageView = [[NetImageView alloc] initWithConfig:config];
_dynamicImageView.layer.masksToBounds = YES;
_dynamicImageView.layer.cornerRadius = 8;
_dynamicImageView.contentMode = UIViewContentModeScaleAspectFill;
}
return _dynamicImageView;
}
- (UILabel *)dynamicLabel {
if (!_dynamicLabel) {
_dynamicLabel = [[UILabel alloc] init];
_dynamicLabel.font = [UIFont systemFontOfSize:12];
_dynamicLabel.textColor = [ThemeColor textThirdColor];
}
return _dynamicLabel;
}
- (UIView *)lineView {
if (!_lineView) {
_lineView = [[UIView alloc] init];
_lineView.backgroundColor = [ThemeColor dividerColor];
}
return _lineView;
}
- (NSDateFormatter *)dataFormatter {
if (!_dataFormatter) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
_dataFormatter = dateFormatter;
}
return _dataFormatter;
}
@end