Files
peko-ios/YuMi/Modules/YMMonents/View/SubViews/XPMonentsContentView.m
2023-10-28 04:46:10 +08:00

195 lines
7.2 KiB
Objective-C

//
// XPMonentsContentView.m
// xplan-ios
//
// Created by 冯硕 on 2022/5/13.
//
#import "XPMonentsContentView.h"
///Third
#import <Masonry/Masonry.h>
///Tool
#import "XPMonentsLayoutConfig.h"
#import "UIButton+EnlargeTouchArea.h"
///Model
#import "MonentsInfoModel.h"
@interface XPMonentsContentView ()
///容器
@property (nonatomic,strong) UIStackView *stackView;
///显示内容
@property (nonatomic,strong) UILabel *contentLabel;
///折叠的按钮
@property (nonatomic,strong) UIButton *foldButton;
///显示时间
@property (nonatomic,strong) UILabel *timeLabel;
@end
@implementation XPMonentsContentView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
[self addSubview:self.stackView];
[self.stackView addArrangedSubview:self.contentLabel];
[self.stackView addArrangedSubview:self.foldButton];
[self.stackView addArrangedSubview:self.timeLabel];
}
- (void)initSubViewConstraints {
[self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self);
}];
[self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(14);
}];
[self.foldButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(kMONENTS_FOLD_HEIGHT);
}];
}
- (NSAttributedString *)createMonentsContentAttribute {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
BOOL isTop = NO;
if(self.isTopic && self.monentsInfo.topicTop){
isTop = YES;
}else{
if (self.monentsInfo.squareTop ) {
isTop = YES;
}
}
if (isTop) {//动态/广场
NSTextAttachment * attachment = [[NSTextAttachment alloc] init];
UIImage *iconImage = [UIImage imageNamed:@"monents_info_top"];;
attachment.bounds = CGRectMake(0, roundf(self.contentLabel.font.capHeight - iconImage.size.height)/2.f, iconImage.size.width, iconImage.size.height);
attachment.image =iconImage;
NSAttributedString * starAttribute = [NSMutableAttributedString attributedStringWithAttachment:(NSTextAttachment *)attachment];
[attributedString insertAttributedString:starAttribute atIndex:0];
//将图片插入到合适的位置
// [attributedString appendAttributedString:[self creatStrAttrByStr:YMLocalizedString(@"XPMonentsContentView0") attributed:@{NSFontAttributeName : [UIFont systemFontOfSize:15], NSForegroundColorAttributeName: UIColorFromRGB(0xE84C46)}]];
}
[attributedString appendAttributedString:[self creatStrAttrByStr:self.monentsInfo.content attributed:@{NSFontAttributeName : [UIFont systemFontOfSize:15]}]];
attributedString.yy_lineSpacing = 5;
attributedString.yy_lineBreakMode = NSLineBreakByWordWrapping;
return attributedString;
}
- (NSMutableAttributedString *)creatStrAttrByStr:(NSString *)str attributed:(NSDictionary *)attribute{
if (str.length == 0 || !str) {
str = @" ";
}
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:str attributes:attribute];
return attr;
}
#pragma mark - Event Response
- (void)didClickFoldButton:(UIButton *)sender {
sender.selected = !sender.selected;
self.monentsInfo.isFold = !sender.selected;
if (self.delegate && [self.delegate respondsToSelector:@selector(xPMonentsContentView:didClickFold:)]) {
[self.delegate xPMonentsContentView:self didClickFold:self.monentsInfo];
}
}
#pragma mark - Getters And Setters
- (void)setMonentsInfo:(MonentsInfoModel *)monentsInfo {
_monentsInfo = monentsInfo;
if (_monentsInfo) {
self.timeLabel.text = [NSString stringWithTimeStamp:_monentsInfo.publishTime];
self.contentLabel.attributedText = [self createMonentsContentAttribute];
self.foldButton.selected = !_monentsInfo.isFold;
if (monentsInfo.numberOfText <= 0) {
YYTextContainer *container = [YYTextContainer new];
container.size = CGSizeMake(kMONENTS_CONTENT_MAX_WIDTH, CGFLOAT_MAX);
container.maximumNumberOfRows = 0;
YYTextLayout * layout = [YYTextLayout layoutWithContainer:container text:self.contentLabel.attributedText];
if (layout.rowCount > 6) {
self.foldButton.hidden = NO;
[self.foldButton mas_remakeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(kMONENTS_FOLD_HEIGHT);
}];
} else {
self.foldButton.hidden = YES;
[self.foldButton mas_remakeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(0);
}];
}
} else {
if (monentsInfo.numberOfText > 6) {
self.foldButton.hidden = NO;
[self.foldButton mas_remakeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(kMONENTS_FOLD_HEIGHT);
}];
} else {
self.foldButton.hidden = YES;
[self.foldButton mas_remakeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(0);
}];
}
}
}
}
- (UIStackView *)stackView {
if (!_stackView) {
_stackView = [[UIStackView alloc] init];
_stackView.axis = UILayoutConstraintAxisVertical;
_stackView.distribution = UIStackViewDistributionFill;
_stackView.alignment = UIStackViewAlignmentLeading;
_stackView.spacing = 5;
}
return _stackView;
}
- (UIButton *)foldButton {
if (_foldButton == nil) {
_foldButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_foldButton setTitle:YMLocalizedString(@"XPMonentsContentView1") forState:UIControlStateNormal];
[_foldButton setTitle:YMLocalizedString(@"XPMonentsContentView2") forState:UIControlStateSelected];
[_foldButton setTitleColor:UIColorFromRGB(0x34A7FF) forState:UIControlStateNormal];
[_foldButton setTitleColor:UIColorFromRGB(0x34A7FF) forState:UIControlStateSelected];
_foldButton.titleLabel.font = [UIFont systemFontOfSize:15];
_foldButton.hidden = YES;
[_foldButton addTarget:self action:@selector(didClickFoldButton:) forControlEvents:UIControlEventTouchUpInside];
}
return _foldButton;
}
- (UILabel *)contentLabel {
if (!_contentLabel) {
_contentLabel = [[UILabel alloc] init];
_contentLabel.numberOfLines = 0;
_contentLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
_contentLabel.textColor = UIColorRGBAlpha(0x1F1A4E, 1);
_contentLabel.preferredMaxLayoutWidth = kMONENTS_CONTENT_MAX_WIDTH;
_contentLabel.lineBreakMode = NSLineBreakByWordWrapping;
}
return _contentLabel;
}
- (UILabel *)timeLabel {
if (!_timeLabel) {
_timeLabel = [[UILabel alloc] init];
_timeLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
_timeLabel.textColor = [DJDKMIMOMColor textThirdColor];
}
return _timeLabel;
}
@end