123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- //
- // MMRichImageCell.m
- // RichTextEditDemo
- //
- // Created by aron on 2017/7/19.
- // Copyright © 2017年 aron. All rights reserved.
- //
- #import "MMRichImageCell.h"
- #import "UtilMacro.h"
- #import "MMRichImageModel.h"
- #import "MMRichTextConfig.h"
- #import "MMTextView.h"
- @interface MMRichImageCell () <MMTextViewDelegate, UITextViewDelegate, MMRichImageUploadDelegate>
- @property (nonatomic, strong) MMTextView* textView;
- @property (nonatomic, strong) UIImageView* imageContentView;
- @property (nonatomic, strong) UIProgressView *progressView;
- @property (nonatomic, strong) TDButton *reloadButton;
- @property (nonatomic, strong) MMRichImageModel* imageModel;
- @end
- @implementation MMRichImageCell
- - (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.imageContentView];
- [self addSubview:self.progressView];
- [self addSubview:self.reloadButton];
-
- [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.top.right.equalTo(self);
- make.bottom.equalTo(self).priority(900);
- }];
- [self.imageContentView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.edges.equalTo(self.textView);
- }];
- [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self).offset(MMEditConfig.editAreaLeftPadding + MMEditConfig.imageDeltaWidth/2);
- make.right.equalTo(self).offset(-(MMEditConfig.editAreaRightPadding + MMEditConfig.imageDeltaWidth/2));
- make.top.equalTo(self).offset(MMEditConfig.editAreaTopPadding);
- make.height.equalTo(@(4));
- }];
- [self.reloadButton mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.mas_equalTo(self.imageContentView.mas_top);
- make.right.mas_equalTo(self.imageContentView.mas_right);
- }];
- [self.reloadButton setCurrentButtonHotSize:CGSizeZero];
- }
- - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
- [super setSelected:selected animated:animated];
-
- }
- - (void)updateWithData:(id)data {
- if ([data isKindOfClass:[MMRichImageModel class]]) {
- MMRichImageModel* imageModel = (MMRichImageModel*)data;
- // 设置旧的数据delegate为nil
- _imageModel.uploadDelegate = nil;
- _imageModel = imageModel;
- // 设置新的数据delegate
- _imageModel.uploadDelegate = self;
-
- CGFloat width = [MMRichTextConfig sharedInstance].editAreaWidth;
- NSAttributedString* imgAttrStr = [_imageModel attrStringWithContainerWidth:width];
- self.textView.attributedText = imgAttrStr;
- self.imageContentView.image = _imageModel.image;
- // 重新设置TextView的约束
- [self.textView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.top.right.equalTo(self);
- make.bottom.equalTo(self).priority(900);
- make.height.equalTo(@(imageModel.imageContentHeight));
- }];
- [self.imageContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self).offset(_imageModel.imageFrame.origin.x);
- make.top.equalTo(self).offset(_imageModel.imageFrame.origin.y);
- make.height.equalTo(@(_imageModel.imageFrame.size.height));
- make.width.equalTo(@(_imageModel.imageFrame.size.width));
- }];
-
- self.reloadButton.hidden = YES;
- self.progressView.hidden = YES;
-
- // 根据上传的状态设置图片信息
- if (_imageModel.isDone) {
- self.progressView.hidden = YES;
- self.progressView.progress = _imageModel.uploadProgress;
- self.reloadButton.hidden = YES;
- } else if (_imageModel.isFailed) {
- self.progressView.hidden = YES;
- self.progressView.progress = _imageModel.uploadProgress;
- self.reloadButton.hidden = NO;
- } else if (_imageModel.uploadProgress > 0) {
- self.progressView.hidden = NO;
- self.progressView.progress = _imageModel.uploadProgress;
- self.reloadButton.hidden = YES;
- }
- }
- }
- - (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;
- }
- - (UIImageView *)imageContentView {
- if (!_imageContentView) {
- _imageContentView = [UIImageView new];
- _imageContentView.contentMode = UIViewContentModeScaleAspectFit;
- _imageContentView.clipsToBounds = YES;
- }
- return _imageContentView;
- }
- - (UIProgressView *)progressView {
- if (!_progressView) {
- _progressView = [UIProgressView new];
- _progressView.progressTintColor = [UIColor redColor];
- _progressView.backgroundColor = [UIColor lightGrayColor];
- }
- return _progressView;
- }
- - (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;
- }
- #pragma mark - ......::::::: MMRichImageUploadDelegate :::::::......
- // 上传进度回调
- - (void)uploadProgress:(float)progress {
- dispatch_async(dispatch_get_main_queue(), ^{
- self.progressView.hidden = NO;
- [self.progressView setProgress:progress];
- self.reloadButton.hidden = YES;
- });
- }
- // 上传失败回调
- - (void)uploadFail {
- dispatch_async(dispatch_get_main_queue(), ^{
- [self.progressView setProgress:0.01f];
- self.reloadButton.hidden = NO;
-
- if ([self.delegate respondsToSelector:@selector(mm_uploadFailedAtIndexPath:)]) {
- [self.delegate mm_uploadFailedAtIndexPath:[self curIndexPath]];
- }
- });
- }
- // 上传完成回调
- - (void)uploadDone {
- dispatch_async(dispatch_get_main_queue(), ^{
- [self.progressView setProgress:1.0f];
- self.progressView.hidden = YES;
- self.reloadButton.hidden = NO;
-
- if ([self.delegate respondsToSelector:@selector(mm_uploadDonedAtIndexPath:)]) {
- [self.delegate mm_uploadDonedAtIndexPath:[self curIndexPath]];
- }
- });
- }
- @end
|