RichFileCell.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //
  2. // RichFileCell.m
  3. // smartRhino
  4. //
  5. // Created by niuzhen on 2020/1/13.
  6. // Copyright © 2020 tederen. All rights reserved.
  7. //
  8. #import "RichFileCell.h"
  9. #import "UtilMacro.h"
  10. #import "MMRichImageModel.h"
  11. #import "MMRichTextConfig.h"
  12. #import "MMTextView.h"
  13. #import "RichModel.h"
  14. @interface RichFileCell () <MMTextViewDelegate, UITextViewDelegate>
  15. @property (nonatomic, strong) MMTextView* textView;
  16. @property (nonatomic, strong) TDButton *reloadButton;
  17. @property (nonatomic, strong) UIView* fileView;
  18. @property (nonatomic, strong) RichModel* fileModel;
  19. @property (nonatomic, strong) UIImageView * iconV;
  20. @property (nonatomic, strong) UILabel* nameL;
  21. @property (nonatomic, strong) UILabel* subTitleL;
  22. @end
  23. @implementation RichFileCell
  24. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  25. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  26. if (self) {
  27. [self setupUI];
  28. }
  29. return self;
  30. }
  31. - (void)setupUI {
  32. [self addSubview:self.textView];
  33. [self addSubview:self.fileView];
  34. [self addSubview:self.reloadButton];
  35. [self.fileView addSubview:self.iconV];
  36. [self.fileView addSubview:self.nameL];
  37. [self.fileView addSubview:self.subTitleL];
  38. [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
  39. make.left.top.right.equalTo(self);
  40. make.bottom.equalTo(self).priority(900);
  41. }];
  42. [self.fileView mas_makeConstraints:^(MASConstraintMaker *make) {
  43. make.edges.mas_offset(UIEdgeInsetsMake(24, 24, 24, 24));
  44. }];
  45. [self.iconV mas_makeConstraints:^(MASConstraintMaker *make) {
  46. make.left.top.mas_offset(@12);
  47. make.size.mas_offset(CGSizeMake(50, 50));
  48. }];
  49. [self.nameL mas_makeConstraints:^(MASConstraintMaker *make) {
  50. make.left.mas_equalTo(self.iconV.mas_right).offset(10);
  51. make.top.mas_offset(20);
  52. }];
  53. [self.subTitleL mas_makeConstraints:^(MASConstraintMaker *make) {
  54. make.left.mas_equalTo(self.iconV.mas_right).offset(10);
  55. make.bottom.mas_offset(-20);
  56. }];
  57. [self.reloadButton mas_makeConstraints:^(MASConstraintMaker *make) {
  58. make.top.mas_equalTo(self.fileView.mas_top);
  59. make.right.mas_equalTo(self.fileView.mas_right);
  60. }];
  61. [self.reloadButton setCurrentButtonHotSize:CGSizeZero];
  62. }
  63. - (void)updateWithData:(id)data {
  64. if([data isKindOfClass:[RichModel class]]){
  65. RichModel* model = (RichModel*)data;
  66. _fileModel = model;
  67. _iconV.image = IMG(@"站内信图");
  68. _nameL.text = model.Title;
  69. _subTitleL.attributedText = [ZYCTool checkOfString:[NSString stringWithFormat:@"来自-%@的收藏",model.Author] KeyString:model.Author];
  70. // 设置旧的数据delegate为nil
  71. NSAttributedString* imgAttrStr = [_fileModel attrStringWithContainer];
  72. self.textView.attributedText = imgAttrStr;
  73. // 重新设置TextView的约束
  74. [self.textView mas_remakeConstraints:^(MASConstraintMaker *make) {
  75. make.left.top.right.equalTo(self);
  76. make.bottom.equalTo(self).priority(900);
  77. make.height.mas_offset(@128);
  78. }];
  79. [self.fileView mas_remakeConstraints:^(MASConstraintMaker *make) {
  80. make.edges.mas_offset(UIEdgeInsetsMake(24, 24, 24, 24));
  81. }];
  82. }
  83. }
  84. - (void)mm_beginEditing {
  85. BOOL result = [self.textView becomeFirstResponder];
  86. // NSLog(@"result = %d", result);
  87. }
  88. - (void)mm_endEditing {
  89. BOOL result = [self.textView resignFirstResponder];
  90. // NSLog(@"result = %d", result);
  91. }
  92. - (void)getPreFlag:(BOOL*)isPre postFlag:(BOOL*)isPost richFlag:(BOOL*)isRich model:(RichModel *)model;
  93. {
  94. NSRange selRange = self.textView.selectedRange;
  95. if (isRich) {
  96. if (model && ![model isKindOfClass:[NSNull class]]) {
  97. *isRich = YES;
  98. }else{
  99. *isRich = NO;
  100. }
  101. }
  102. // 设置标记值
  103. if (isPre) {
  104. if (selRange.location == 0) {
  105. *isPre = YES;
  106. } else {
  107. *isPre = NO;
  108. }
  109. }
  110. if (isPost) {
  111. if (selRange.location+selRange.length == _textView.text.length) {
  112. *isPost = YES;
  113. } else {
  114. *isPost = NO;
  115. }
  116. }
  117. }
  118. #pragma mark - ......::::::: lazy load :::::::......
  119. - (MMTextView *)textView {
  120. if (!_textView) {
  121. _textView = [MMTextView new];
  122. _textView.font = MMEditConfig.defaultEditContentFont;
  123. _textView.textContainerInset = UIEdgeInsetsMake(MMEditConfig.editAreaTopPadding, MMEditConfig.editAreaLeftPadding, MMEditConfig.editAreaBottomPadding, MMEditConfig.editAreaRightPadding);
  124. _textView.scrollEnabled = NO;
  125. _textView.allowsEditingTextAttributes = YES;
  126. _textView.delegate = self;
  127. _textView.mm_delegate = self;
  128. }
  129. return _textView;
  130. }
  131. - (UIView *)fileView
  132. {
  133. if (!_fileView) {
  134. _fileView = [UIView new];
  135. _fileView.backgroundColor = UIColorHex(#F4F5F6);
  136. }
  137. return _fileView;
  138. }
  139. - (UIImageView *)iconV
  140. {
  141. if (!_iconV) {
  142. _iconV = [UIImageView new];
  143. _iconV.layer.cornerRadius = 4.f;
  144. _iconV.layer.masksToBounds = YES;
  145. }
  146. return _iconV;
  147. }
  148. - (UILabel *)nameL
  149. {
  150. if (!_nameL) {
  151. _nameL = [[UILabel alloc] init];
  152. _nameL.textColor = UIColorHex(#0A0A0A);
  153. _nameL.font = [UIFont systemFontOfSize:16];
  154. }
  155. return _nameL;
  156. }
  157. - (UILabel *)subTitleL
  158. {
  159. if (!_subTitleL) {
  160. _subTitleL = [[UILabel alloc] init];
  161. _subTitleL.textColor = UIColorHex(#999999);
  162. _subTitleL.font = [UIFont systemFontOfSize:11];
  163. }
  164. return _subTitleL;
  165. }
  166. - (TDButton *)reloadButton {
  167. if (!_reloadButton) {
  168. _reloadButton = [[TDButton alloc] init];
  169. [_reloadButton setImage:[UIImage imageNamed:@"频道关闭"] forState:UIControlStateNormal];
  170. [_reloadButton addTarget:self action:@selector(onReloadBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  171. }
  172. return _reloadButton;
  173. }
  174. - (void)onReloadBtnClick:(UIButton*)sender {
  175. if ([self.delegate respondsToSelector:@selector(mm_reloadItemAtIndexPath:)]) {
  176. [self.delegate mm_reloadItemAtIndexPath:[self curIndexPath]];
  177. }
  178. }
  179. #pragma mark - ......::::::: UITextViewDelegate :::::::......
  180. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  181. // 处理换行
  182. if ([text isEqualToString:@"\n"] || text.length > 0) {
  183. if (range.location == 0 && range.length == 0) {
  184. // 在前面添加换行
  185. if ([self.delegate respondsToSelector:@selector(mm_preInsertTextLineAtIndexPath:textContent:)]) {
  186. [self.delegate mm_preInsertTextLineAtIndexPath:[self curIndexPath]textContent:text];
  187. }
  188. } else if (range.location == 1 && range.length == 0) {
  189. // 在后面添加换行
  190. if ([self.delegate respondsToSelector:@selector(mm_postInsertTextLineAtIndexPath:textContent:)]) {
  191. [self.delegate mm_postInsertTextLineAtIndexPath:[self curIndexPath] textContent:text];
  192. }
  193. } else if (range.location == 0 && range.length == 2) {
  194. // 选中和换行
  195. }
  196. }
  197. if (![text isEqualToString:@"\n"] && text.length > 0) {
  198. if (range.location == 1 && range.length == 0) {
  199. // 在后面添加换行
  200. if ([self.delegate respondsToSelector:@selector(mm_postInsertTextLineAtIndexPath:textContent:)]) {
  201. [self.delegate mm_postInsertTextLineAtIndexPath:[self curIndexPath] textContent:text];
  202. }
  203. }
  204. }
  205. // 处理删除
  206. if ([text isEqualToString:@""]) {
  207. NSRange selRange = textView.selectedRange;
  208. if (selRange.location == 0 && selRange.length == 0) {
  209. // 处理删除
  210. if ([self.delegate respondsToSelector:@selector(mm_preDeleteItemAtIndexPath:)]) {
  211. [self.delegate mm_preDeleteItemAtIndexPath:[self curIndexPath]];
  212. }
  213. } else if (selRange.location == 1 && selRange.length == 0) {
  214. // 处理删除
  215. if ([self.delegate respondsToSelector:@selector(mm_PostDeleteItemAtIndexPath:)]) {
  216. [self.delegate mm_PostDeleteItemAtIndexPath:[self curIndexPath]];
  217. }
  218. } else if (selRange.location == 0 && selRange.length == 2) {
  219. // 处理删除
  220. if ([self.delegate respondsToSelector:@selector(mm_preDeleteItemAtIndexPath:)]) {
  221. [self.delegate mm_preDeleteItemAtIndexPath:[self curIndexPath]];
  222. }
  223. }
  224. }
  225. return NO;
  226. }
  227. - (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
  228. // textView.inputAccessoryView = [self.delegate mm_inputAccessoryView];
  229. if ([self.delegate respondsToSelector:@selector(mm_shouldShowAccessoryView:)]) {
  230. [self.delegate mm_shouldShowAccessoryView:YES];
  231. }
  232. if ([self.delegate respondsToSelector:@selector(mm_updateActiveIndexPath:)]) {
  233. [self.delegate mm_updateActiveIndexPath:[self curIndexPath]];
  234. }
  235. return YES;
  236. }
  237. - (BOOL)textViewShouldEndEditing:(UITextView *)textView {
  238. textView.inputAccessoryView = nil;
  239. return YES;
  240. }
  241. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  242. [super setSelected:selected animated:animated];
  243. // Configure the view for the selected state
  244. }
  245. @end