Files
yinmeng-ios/xplan-ios/Main/Monents/View/SubViews/XPMonentsContentView.m
2022-08-26 21:47:28 +08:00

167 lines
5.5 KiB
Objective-C

//
// XPMonentsContentView.m
// xplan-ios
//
// Created by 冯硕 on 2022/5/13.
//
#import "XPMonentsContentView.h"
///Third
#import <Masonry/Masonry.h>
#import <YYText/YYText.h>
///Tool
#import "ThemeColor.h"
#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;
@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];
}
- (void)initSubViewConstraints {
[self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self);
}];
[self.foldButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(kMONENTS_FOLD_HEIGHT);
}];
}
- (NSAttributedString *)createMonentsContentAttribute {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
if (self.monentsInfo.squareTop ) {//动态/广场
NSTextAttachment * attachment = [[NSTextAttachment alloc] init];
attachment.bounds = CGRectMake(0, 0, 10, 12);
attachment.image = [UIImage imageNamed:@"monents_info_top"];
NSAttributedString * starAttribute = [NSMutableAttributedString attributedStringWithAttachment:(NSTextAttachment *)attachment];
[attributedString insertAttributedString:starAttribute atIndex:0];
//将图片插入到合适的位置
[attributedString appendAttributedString:[self creatStrAttrByStr:@"置顶 " attributed:@{NSFontAttributeName : [UIFont systemFontOfSize:15], NSForegroundColorAttributeName: UIColorFromRGB(0xE84C46)}]];
}
[attributedString appendAttributedString:[self creatStrAttrByStr:self.monentsInfo.content attributed:@{NSFontAttributeName : [UIFont systemFontOfSize:15]}]];
attributedString.yy_lineSpacing = 5;
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.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;
}
return _stackView;
}
- (UIButton *)foldButton {
if (_foldButton == nil) {
_foldButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_foldButton setTitle:@"展开" forState:UIControlStateNormal];
[_foldButton setTitle:@"收起" 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:15];
_contentLabel.textColor = UIColorRGBAlpha(0x333333, 1);
_contentLabel.preferredMaxLayoutWidth = kMONENTS_CONTENT_MAX_WIDTH;
_contentLabel.lineBreakMode = NSLineBreakByCharWrapping;
}
return _contentLabel;
}
@end