123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- //
- // MMRichTitleCell.m
- // RichTextEditDemo
- //
- // Created by aron on 2017/7/21.
- // Copyright © 2017年 aron. All rights reserved.
- //
- #import "MMRichTitleCell.h"
- #import "MMTextView.h"
- #import "MMRichTitleModel.h"
- #import <Masonry.h>
- #import "MMRichTextConfig.h"
- #import "UtilMacro.h"
- @interface MMRichTitleCell () <MMTextViewDelegate, UITextViewDelegate>
- @property (nonatomic, strong) MMTextView* textView;
- @property (nonatomic, strong) UIView* separatorView;
- @property (nonatomic, strong) MMRichTitleModel* titleModel;
- @property (nonatomic, assign) BOOL isEditing;
- @end
- @implementation MMRichTitleCell
- - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
- self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
- if (self) {
- [self setupUI];
- }
- return self;
- }
- - (void)dealloc {
- _textView.delegate = nil;
- [[NSNotificationCenter defaultCenter ] removeObserver:self];
- }
- - (void)setupUI {
- [self addSubview:self.textView];
- [self addSubview:self.separatorView];
-
- [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.top.right.equalTo(self);
- make.bottom.equalTo(self).priority(900);
- }];
- [self.separatorView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self).offset(convertLength(15));
- make.right.equalTo(self).offset(-convertLength(15));
- make.bottom.equalTo(self).offset(-convertLength(0.5));
- make.height.equalTo(@(convertLength(7.5)));
- }];
-
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:nil];
- }
- - (void)updateWithData:(id)data indexPath:(NSIndexPath*)indexPath {
- if ([data isKindOfClass:[MMRichTitleModel class]]) {
- MMRichTitleModel* titleModel = (MMRichTitleModel*)data;
- _titleModel = titleModel;
-
- // 重新设置TextView的约束
- [self.textView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.top.right.equalTo(self);
- make.bottom.equalTo(self).priority(900);
- make.height.equalTo(@(_titleModel.titleContentHeight));
- }];
-
- // Content
- self.textView.text = _titleModel.textContent;
- }
- }
- - (void)mm_beginEditing {
- [self.textView becomeFirstResponder];
-
- if (![self.textView.text isEqualToString:_titleModel.textContent]) {
- self.textView.text = _titleModel.textContent;
-
- // 手动调用回调方法修改
- [self textViewDidChange:self.textView];
- }
- }
- - (void)mm_endEditing {
- BOOL result = [self.textView resignFirstResponder];
- // NSLog(@"result = %d", result);
- }
- #pragma mark - ......::::::: lazy load :::::::......
- - (MMTextView *)textView {
- if (!_textView) {
- _textView = [MMTextView new];
- _textView.font = MMEditConfig.defaultEditTitleFont;
- _textView.textContainerInset = UIEdgeInsetsMake(MMEditConfig.editTitleAreaTopPadding, MMEditConfig.editTitleAreaLeftPadding, MMEditConfig.editTitleAreaBottomPadding, MMEditConfig.editTitleAreaRightPadding);
- _textView.scrollEnabled = NO;
- _textView.maxInputs = MMEditConfig.titleMaxCount;
- _textView.placeHolder = @"标题:";
- _textView.placeHolderColor = UIColorHex(#999999);
- _textView.showPlaceHolder = YES;
- _textView.delegate = self;
- _textView.mm_delegate = self;
- _textView.textColor = UIColorHex(#0A0A0A);
- }
- return _textView;
- }
- - (UIView *)separatorView {
- if (!_separatorView) {
- _separatorView = [UIView new];
- _separatorView.backgroundColor = [UIColor whiteColor];
-
- UIView* separatorLine = [UIView new];
- separatorLine.backgroundColor = UIColorHex(#C7E4FB);
- [_separatorView addSubview:separatorLine];
- [separatorLine mas_makeConstraints:^(MASConstraintMaker *make) {
- make.height.equalTo(@(convertLength(0.5)));
- make.left.top.right.equalTo(_separatorView);
- }];
- }
- return _separatorView;
- }
- #pragma mark - ......::::::: UITextViewDelegate :::::::......
- - (void)textViewDidChange:(UITextView *)textView {
-
- }
- - (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
- if ([self.delegate respondsToSelector:@selector(mm_shouldShowAccessoryView:)]) {
- [self.delegate mm_shouldShowAccessoryView:NO];
- }
- self.isEditing = YES;
- return YES;
- }
- - (BOOL)textViewShouldEndEditing:(UITextView *)textView {
- self.isEditing = NO;
- return YES;
- }
- - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
- NSLog(@"");
- // 处理删除,防止输入的字数为最大值的时候删除无效
- if ([text isEqualToString:@""]) {
- return YES;
- }
- if (NO == self.isEditing) {
- // 隐藏键盘,TextView会自动填充选中的联想词,这个地方返回NO做特殊处理,
- // 不让TextView自动填充选中的联想词
- self.isEditing = YES;
- return NO;
- }
- // 中间位置不能插入更多导致超过最大值
- // 结尾位置支持插入更多 && range.location < textView.text.length
- if (textView.text.length + text.length > MMEditConfig.titleMaxCount) {
- return NO;
- }
- return YES;
- }
- - (void)handleTextViewDidChange {
- CGRect frame = self.textView.frame;
- CGSize constraintSize = CGSizeMake(frame.size.width, MAXFLOAT);
- CGSize size = [self.textView sizeThatFits:constraintSize];
-
- // 更新模型数据
- _titleModel.titleContentHeight = size.height;
- _titleModel.textContent = self.textView.text;
- _titleModel.selectedRange = self.textView.selectedRange;
- _titleModel.isEditing = YES;
-
- if (ABS(_textView.frame.size.height - size.height) > 5) {
- // 重新设置TextView的约束
- [self.textView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.top.right.equalTo(self);
- make.bottom.equalTo(self).priority(900);
- make.height.equalTo(@(_titleModel.titleContentHeight));
- }];
-
- UITableView* tableView = [self containerTableView];
- [tableView beginUpdates];
- [tableView endUpdates];
- }
- }
- #pragma mark - ......::::::: notification :::::::......
- - (void)textDidChange:(NSNotification*)notification {
- NSObject* obj = notification.object;
- if (obj == self.textView) {
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [self handleTextViewDidChange];
- });
- }
- }
- @end
|