MMRichTextCell.m 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // MMRichTextCell.m
  3. // RichTextEditDemo
  4. //
  5. // Created by aron on 2017/7/19.
  6. // Copyright © 2017年 aron. All rights reserved.
  7. //
  8. #import "MMRichTextCell.h"
  9. #import <Masonry.h>
  10. #import "MMRichTextModel.h"
  11. #import "MMTextView.h"
  12. #import "MMRichTextConfig.h"
  13. #import "UtilMacro.h"
  14. #import "UITextView+RCSBackWord.h"
  15. @interface MMRichTextCell () <MMTextViewDelegate, UITextViewDelegate>
  16. @property (nonatomic, strong) MMTextView* textView;
  17. @property (nonatomic, strong) MMRichTextModel* textModel;
  18. @property (nonatomic, strong) RichModel* fileModel;
  19. @property (nonatomic, assign) BOOL isEditing;
  20. @end
  21. @implementation MMRichTextCell
  22. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  23. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  24. if (self) {
  25. [self setupUI];
  26. }
  27. return self;
  28. }
  29. - (void)dealloc {
  30. _textView.delegate = nil;
  31. }
  32. - (void)setupUI {
  33. [self addSubview:self.textView];
  34. [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
  35. make.left.top.right.equalTo(self);
  36. make.bottom.equalTo(self).priority(900);
  37. }];
  38. }
  39. #pragma mark - ......::::::: public :::::::......
  40. - (void)updateWithData:(id)data indexPath:(NSIndexPath*)indexPath {
  41. if ([data isKindOfClass:[MMRichTextModel class]]) {
  42. MMRichTextModel* textModel = (MMRichTextModel*)data;
  43. _textModel = textModel;
  44. // 重新设置TextView的约束
  45. [self.textView mas_remakeConstraints:^(MASConstraintMaker *make) {
  46. make.left.top.right.equalTo(self);
  47. make.bottom.equalTo(self).priority(900);
  48. make.height.equalTo(@(textModel.textContentHeight));
  49. }];
  50. // Content
  51. _textView.text = textModel.textContent;
  52. // Placeholder
  53. [self handlePlaceholder];
  54. }
  55. }
  56. - (void)mm_beginEditing {
  57. [_textView becomeFirstResponder];
  58. _textView.text = _textModel.textContent;
  59. if (_textModel.shouldUpdateSelectedRange) {
  60. // 手动调用回调方法修改
  61. [self textViewDidChange:_textView];
  62. }
  63. // Placeholder
  64. [self handlePlaceholder];
  65. // 回调
  66. if ([self.delegate respondsToSelector:@selector(mm_updateActiveIndexPath:)]) {
  67. [self.delegate mm_updateActiveIndexPath:[self curIndexPath]];
  68. }
  69. }
  70. - (void)mm_endEditing {
  71. BOOL result = [self.textView resignFirstResponder];
  72. // NSLog(@"result = %d", result);
  73. }
  74. - (NSRange)selectRange {
  75. return _textView.selectedRange;
  76. }
  77. - (NSArray<NSString*>*)splitedTextArrWithPreFlag:(BOOL*)isPre postFlag:(BOOL*)isPost richFlag:(BOOL*)isRich model:(RichModel *)model{
  78. NSMutableArray* splitedTextArr = [NSMutableArray new];
  79. NSRange selRange = self.selectRange;
  80. // 设置标记值
  81. if (isPre) {
  82. if (selRange.location == 0) {
  83. *isPre = YES;
  84. } else {
  85. *isPre = NO;
  86. }
  87. }
  88. if (isRich) {
  89. if (model && ![model isKindOfClass:[NSNull class]]) {
  90. *isRich = YES;
  91. }else{
  92. *isRich = NO;
  93. }
  94. }
  95. if (isPost) {
  96. if (selRange.location+selRange.length == _textView.text.length) {
  97. *isPost = YES;
  98. } else {
  99. *isPost = NO;
  100. }
  101. }
  102. // 0 - selectRange.location
  103. if (selRange.location > 0) {
  104. [splitedTextArr addObject:[_textView.text substringToIndex:selRange.location]];
  105. }
  106. // selectRange.location+selectRange.length - end
  107. if (selRange.location+selRange.length < _textView.text.length) {
  108. [splitedTextArr addObject:[_textView.text substringWithRange:NSMakeRange(selRange.location+selRange.length, _textView.text.length - (selRange.location+selRange.length))]];
  109. }
  110. return splitedTextArr;
  111. }
  112. #pragma mark - ......::::::: lazy load :::::::......
  113. - (MMTextView *)textView {
  114. if (!_textView) {
  115. _textView = [MMTextView new];
  116. _textView.font = MMEditConfig.defaultEditContentFont;
  117. _textView.textContainerInset = UIEdgeInsetsMake(MMEditConfig.editAreaTopPadding, MMEditConfig.editAreaLeftPadding, MMEditConfig.editAreaBottomPadding, MMEditConfig.editAreaRightPadding);
  118. _textView.scrollEnabled = NO;
  119. _textView.placeHolder = @"编辑内容";
  120. _textView.placeHolderColor = UIColorHex(#999999);
  121. _textView.maxInputs = MMEditConfig.maxTextContentCount;
  122. _textView.delegate = self;
  123. _textView.mm_delegate = self;
  124. _textView.textColor = UIColorHex(#0A0A0A);
  125. }
  126. return _textView;
  127. }
  128. #pragma mark - ......::::::: private :::::::......
  129. - (void)scrollToCursorForTextView:(UITextView*)textView {
  130. CGRect cursorRect = [textView caretRectForPosition:textView.selectedTextRange.start];
  131. cursorRect = [self.containerTableView convertRect:cursorRect fromView:textView];
  132. if (![self rectVisible:cursorRect]) {
  133. cursorRect.size.height += 8; // To add some space underneath the cursor
  134. [self.containerTableView scrollRectToVisible:cursorRect animated:YES];
  135. }
  136. }
  137. - (BOOL)rectVisible:(CGRect)rect {
  138. CGRect visibleRect;
  139. visibleRect.origin = self.containerTableView.contentOffset;
  140. visibleRect.origin.y += self.containerTableView.contentInset.top;
  141. visibleRect.size = self.containerTableView.bounds.size;
  142. visibleRect.size.height -= self.containerTableView.contentInset.top + self.containerTableView.contentInset.bottom;
  143. return CGRectContainsRect(visibleRect, rect);
  144. }
  145. - (void)handlePlaceholder {
  146. if ([self.delegate respondsToSelector:@selector(mm_shouldCellShowPlaceholder)]) {
  147. BOOL showPlaceholder = [self.delegate mm_shouldCellShowPlaceholder];
  148. self.textView.showPlaceHolder = showPlaceholder;
  149. } else {
  150. self.textView.showPlaceHolder = NO;
  151. }
  152. }
  153. #pragma mark - ......::::::: UITextViewDelegate :::::::......
  154. - (void)textViewDidChange:(UITextView *)textView {
  155. CGRect frame = textView.frame;
  156. CGSize constraintSize = CGSizeMake(frame.size.width, MAXFLOAT);
  157. CGSize size = [textView sizeThatFits:constraintSize];
  158. // 更新模型数据
  159. _textModel.textContentHeight = size.height;
  160. _textModel.textContent = textView.text;
  161. if (_textModel.shouldUpdateSelectedRange) {
  162. // 光标位置特殊处理
  163. textView.selectedRange = _textModel.selectedRange;
  164. _textModel.shouldUpdateSelectedRange = NO;
  165. }
  166. _textModel.isEditing = YES;
  167. if (ABS(_textView.frame.size.height - size.height) > 5) {
  168. UITableView* tableView = [self containerTableView];
  169. [tableView beginUpdates];
  170. // 重新设置TextView的约束
  171. [self.textView mas_remakeConstraints:^(MASConstraintMaker *make) {
  172. make.left.top.right.equalTo(self);
  173. make.bottom.equalTo(self).priority(900);
  174. make.height.equalTo(@(_textModel.textContentHeight));
  175. }];
  176. [tableView endUpdates];
  177. // 移动光标 https://stackoverflow.com/questions/18368567/uitableviewcell-with-uitextview-height-in-ios-7
  178. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  179. [self scrollToCursorForTextView:textView];
  180. });
  181. }
  182. if (textView.text.length <= 0) {
  183. // Placeholder
  184. [self handlePlaceholder];
  185. }
  186. }
  187. - (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
  188. if ([self.delegate respondsToSelector:@selector(mm_shouldShowAccessoryView:)]) {
  189. [self.delegate mm_shouldShowAccessoryView:YES];
  190. }
  191. self.isEditing = YES;
  192. if ([self.delegate respondsToSelector:@selector(mm_updateActiveIndexPath:)]) {
  193. [self.delegate mm_updateActiveIndexPath:[self curIndexPath]];
  194. }
  195. return YES;
  196. }
  197. - (BOOL)textViewShouldEndEditing:(UITextView *)textView {
  198. textView.inputAccessoryView = nil;
  199. self.isEditing = NO;
  200. return YES;
  201. }
  202. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  203. NSLog(@"");
  204. // 处理删除,防止输入的字数为最大值的时候删除无效
  205. if ([text isEqualToString:@""]) {
  206. return YES;
  207. }
  208. if (NO == self.isEditing) {
  209. // 隐藏键盘,TextView会自动填充选中的联想词,这个地方返回NO做特殊处理,
  210. // 不让TextView自动填充选中的联想词
  211. self.isEditing = YES;
  212. return NO;
  213. }
  214. // 中间位置不能插入更多导致超过最大值
  215. // 结尾位置支持插入更多 && range.location < textView.text.length
  216. if (textView.text.length + text.length > MMEditConfig.maxTextContentCount) {
  217. return NO;
  218. }
  219. return YES;
  220. }
  221. #pragma mark delete handler
  222. - (void)textViewWillDelete {
  223. // 处理的删除
  224. NSRange selRange = self.textView.selectedRange;
  225. if (selRange.location == 0) {
  226. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  227. if ([self.delegate respondsToSelector:@selector(mm_preDeleteItemAtIndexPath:)]) {
  228. [self.delegate mm_preDeleteItemAtIndexPath:[self curIndexPath]];
  229. }
  230. });
  231. } else {
  232. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  233. if ([self.delegate respondsToSelector:@selector(mm_PostDeleteItemAtIndexPath:)]) {
  234. [self.delegate mm_PostDeleteItemAtIndexPath:[self curIndexPath]];
  235. }
  236. });
  237. }
  238. }
  239. @end