123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- //
- // RichFileCell.m
- // smartRhino
- //
- // Created by niuzhen on 2020/1/13.
- // Copyright © 2020 tederen. All rights reserved.
- //
- #import "RichFileCell.h"
- #import "UtilMacro.h"
- #import "MMRichImageModel.h"
- #import "MMRichTextConfig.h"
- #import "MMTextView.h"
- #import "RichModel.h"
- @interface RichFileCell () <MMTextViewDelegate, UITextViewDelegate>
- @property (nonatomic, strong) MMTextView* textView;
- @property (nonatomic, strong) TDButton *reloadButton;
- @property (nonatomic, strong) UIView* fileView;
- @property (nonatomic, strong) RichModel* fileModel;
- @property (nonatomic, strong) UIImageView * iconV;
- @property (nonatomic, strong) UILabel* nameL;
- @property (nonatomic, strong) UILabel* subTitleL;
- @end
- @implementation RichFileCell
- - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
- self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
- if (self) {
- [self setupUI];
- }
- return self;
- }
- - (void)setupUI {
- [self addSubview:self.textView];
- [self addSubview:self.fileView];
- [self addSubview:self.reloadButton];
- [self.fileView addSubview:self.iconV];
- [self.fileView addSubview:self.nameL];
- [self.fileView addSubview:self.subTitleL];
- [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.top.right.equalTo(self);
- make.bottom.equalTo(self).priority(900);
- }];
- [self.fileView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.edges.mas_offset(UIEdgeInsetsMake(24, 24, 24, 24));
- }];
- [self.iconV mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.top.mas_offset(@12);
- make.size.mas_offset(CGSizeMake(50, 50));
- }];
- [self.nameL mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(self.iconV.mas_right).offset(10);
- make.top.mas_offset(20);
- }];
- [self.subTitleL mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(self.iconV.mas_right).offset(10);
- make.bottom.mas_offset(-20);
- }];
- [self.reloadButton mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.mas_equalTo(self.fileView.mas_top);
- make.right.mas_equalTo(self.fileView.mas_right);
- }];
- [self.reloadButton setCurrentButtonHotSize:CGSizeZero];
- }
- - (void)updateWithData:(id)data {
- if([data isKindOfClass:[RichModel class]]){
- RichModel* model = (RichModel*)data;
- _fileModel = model;
- _iconV.image = IMG(@"站内信图");
- _nameL.text = model.Title;
- _subTitleL.attributedText = [ZYCTool checkOfString:[NSString stringWithFormat:@"来自-%@的收藏",model.Author] KeyString:model.Author];
- // 设置旧的数据delegate为nil
- NSAttributedString* imgAttrStr = [_fileModel attrStringWithContainer];
-
- self.textView.attributedText = imgAttrStr;
- // 重新设置TextView的约束
- [self.textView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.top.right.equalTo(self);
- make.bottom.equalTo(self).priority(900);
- make.height.mas_offset(@128);
- }];
- [self.fileView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.edges.mas_offset(UIEdgeInsetsMake(24, 24, 24, 24));
- }];
- }
- }
- - (void)mm_beginEditing {
- BOOL result = [self.textView becomeFirstResponder];
- // NSLog(@"result = %d", result);
- }
- - (void)mm_endEditing {
- BOOL result = [self.textView resignFirstResponder];
- // NSLog(@"result = %d", result);
- }
- - (void)getPreFlag:(BOOL*)isPre postFlag:(BOOL*)isPost richFlag:(BOOL*)isRich model:(RichModel *)model;
- {
- NSRange selRange = self.textView.selectedRange;
- if (isRich) {
- if (model && ![model isKindOfClass:[NSNull class]]) {
- *isRich = YES;
- }else{
- *isRich = NO;
- }
- }
- // 设置标记值
- if (isPre) {
- if (selRange.location == 0) {
- *isPre = YES;
- } else {
- *isPre = NO;
- }
- }
-
- if (isPost) {
- if (selRange.location+selRange.length == _textView.text.length) {
- *isPost = YES;
- } else {
- *isPost = NO;
- }
- }
- }
- #pragma mark - ......::::::: lazy load :::::::......
- - (MMTextView *)textView {
- if (!_textView) {
- _textView = [MMTextView new];
- _textView.font = MMEditConfig.defaultEditContentFont;
- _textView.textContainerInset = UIEdgeInsetsMake(MMEditConfig.editAreaTopPadding, MMEditConfig.editAreaLeftPadding, MMEditConfig.editAreaBottomPadding, MMEditConfig.editAreaRightPadding);
- _textView.scrollEnabled = NO;
- _textView.allowsEditingTextAttributes = YES;
- _textView.delegate = self;
- _textView.mm_delegate = self;
- }
- return _textView;
- }
- - (UIView *)fileView
- {
- if (!_fileView) {
- _fileView = [UIView new];
- _fileView.backgroundColor = UIColorHex(#F4F5F6);
- }
- return _fileView;
- }
- - (UIImageView *)iconV
- {
- if (!_iconV) {
- _iconV = [UIImageView new];
- _iconV.layer.cornerRadius = 4.f;
- _iconV.layer.masksToBounds = YES;
- }
- return _iconV;
- }
- - (UILabel *)nameL
- {
- if (!_nameL) {
- _nameL = [[UILabel alloc] init];
- _nameL.textColor = UIColorHex(#0A0A0A);
- _nameL.font = [UIFont systemFontOfSize:16];
- }
- return _nameL;
- }
- - (UILabel *)subTitleL
- {
- if (!_subTitleL) {
- _subTitleL = [[UILabel alloc] init];
- _subTitleL.textColor = UIColorHex(#999999);
- _subTitleL.font = [UIFont systemFontOfSize:11];
- }
- return _subTitleL;
- }
- - (TDButton *)reloadButton {
- if (!_reloadButton) {
- _reloadButton = [[TDButton alloc] init];
- [_reloadButton setImage:[UIImage imageNamed:@"频道关闭"] forState:UIControlStateNormal];
- [_reloadButton addTarget:self action:@selector(onReloadBtnClick:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _reloadButton;
- }
- - (void)onReloadBtnClick:(UIButton*)sender {
- if ([self.delegate respondsToSelector:@selector(mm_reloadItemAtIndexPath:)]) {
- [self.delegate mm_reloadItemAtIndexPath:[self curIndexPath]];
- }
- }
- #pragma mark - ......::::::: UITextViewDelegate :::::::......
- - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
- // 处理换行
- if ([text isEqualToString:@"\n"] || text.length > 0) {
- if (range.location == 0 && range.length == 0) {
- // 在前面添加换行
- if ([self.delegate respondsToSelector:@selector(mm_preInsertTextLineAtIndexPath:textContent:)]) {
- [self.delegate mm_preInsertTextLineAtIndexPath:[self curIndexPath]textContent:text];
- }
- } else if (range.location == 1 && range.length == 0) {
- // 在后面添加换行
- if ([self.delegate respondsToSelector:@selector(mm_postInsertTextLineAtIndexPath:textContent:)]) {
- [self.delegate mm_postInsertTextLineAtIndexPath:[self curIndexPath] textContent:text];
- }
- } else if (range.location == 0 && range.length == 2) {
- // 选中和换行
- }
- }
-
- if (![text isEqualToString:@"\n"] && text.length > 0) {
- if (range.location == 1 && range.length == 0) {
- // 在后面添加换行
- if ([self.delegate respondsToSelector:@selector(mm_postInsertTextLineAtIndexPath:textContent:)]) {
- [self.delegate mm_postInsertTextLineAtIndexPath:[self curIndexPath] textContent:text];
- }
- }
- }
-
- // 处理删除
- if ([text isEqualToString:@""]) {
- NSRange selRange = textView.selectedRange;
- if (selRange.location == 0 && selRange.length == 0) {
- // 处理删除
- if ([self.delegate respondsToSelector:@selector(mm_preDeleteItemAtIndexPath:)]) {
- [self.delegate mm_preDeleteItemAtIndexPath:[self curIndexPath]];
- }
- } else if (selRange.location == 1 && selRange.length == 0) {
- // 处理删除
- if ([self.delegate respondsToSelector:@selector(mm_PostDeleteItemAtIndexPath:)]) {
- [self.delegate mm_PostDeleteItemAtIndexPath:[self curIndexPath]];
- }
- } else if (selRange.location == 0 && selRange.length == 2) {
- // 处理删除
- if ([self.delegate respondsToSelector:@selector(mm_preDeleteItemAtIndexPath:)]) {
- [self.delegate mm_preDeleteItemAtIndexPath:[self curIndexPath]];
- }
- }
- }
- return NO;
- }
- - (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
- // textView.inputAccessoryView = [self.delegate mm_inputAccessoryView];
- if ([self.delegate respondsToSelector:@selector(mm_shouldShowAccessoryView:)]) {
- [self.delegate mm_shouldShowAccessoryView:YES];
- }
- if ([self.delegate respondsToSelector:@selector(mm_updateActiveIndexPath:)]) {
- [self.delegate mm_updateActiveIndexPath:[self curIndexPath]];
- }
- return YES;
- }
- - (BOOL)textViewShouldEndEditing:(UITextView *)textView {
- textView.inputAccessoryView = nil;
- return YES;
- }
- - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
- [super setSelected:selected animated:animated];
- // Configure the view for the selected state
- }
- @end
|