仿豆瓣日記編輯功能,實(shí)現(xiàn)了圖文編輯,可以插入刪除圖片,給圖片加標(biāo)題,并且長(zhǎng)按可以移動(dòng)圖片位置

初始化TextView數(shù)據(jù)
- (void)p_setData:(NSString *)data;
該方法里暫時(shí)只對(duì)<img full_url="https://dn-qinqinwojia.qbox.me/Fo1cGsOJ-QArC4-pH9-PoG1nfHKo" abbr_url="" caption="haha" />這一種個(gè)是進(jìn)行了處理,如果需要處理其他格式可以在這個(gè)方法里添加。
富文本操作
- (NSAttributedString *)p_textViewAttributedText:(id)attribute contentText:(NSAttributedString *)attributeString index:(NSInteger)index originPoint:(CGPoint)originPoint isData:(BOOL)isData {
NSMutableAttributedString *contentText = [attributeString mutableCopy];
NSAttributedString *textAttachmentString = [[NSAttributedString alloc] initWithString:@"\n"];
if ([attribute isKindOfClass:[JQLImageView class]]) {
JQLImageView *imageView = (JQLImageView *)attribute;
CGFloat imageViewHeight = ![imageView.title isEqualToString:@""] ? IMAGE_WIDTH + 30.0 : IMAGE_WIDTH;
imageView.frame = CGRectMake(originPoint.x, originPoint.y, IMAGE_WIDTH, imageViewHeight);
NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:imageView contentMode:UIViewContentModeScaleAspectFit attachmentSize:imageView.frame.size alignToFont:_textView.font alignment:YYTextVerticalAlignmentCenter];
if (!isData) [contentText insertAttributedString:textAttachmentString atIndex:index++];
[contentText insertAttributedString:attachText atIndex:index++];
imageView.editBlock = ^(JQLImageView *imageView) {
[self p_editImageViewTitle:imageView point:imageView.frame.origin];
};
imageView.moveBlock = ^(JQLImageView *imageView, UILongPressGestureRecognizer *longPress) {
[self p_move:imageView longPress:longPress];
};
imageView.deleteBlock = ^(JQLImageView *imageView) {
[self p_deleteImageView:imageView];
};
}
return contentText;
}
該方法將圖片先轉(zhuǎn)成了自己定義的一個(gè)ImageView視圖,然后將視圖,轉(zhuǎn)成AttributedText,對(duì)_textView進(jìn)行賦值。
如果需要獲取富文本當(dāng)中的視圖,可以通過(guò)_textView.textLayout.attachments來(lái)進(jìn)行獲取。該數(shù)組里存放了所有的YYTextAttachment,YYTextAttachment里content屬性是id類(lèi)型,可以直接轉(zhuǎn)成相應(yīng)的視圖。另外還有一個(gè)對(duì)應(yīng)的_textView.textLayout.attachmentRanges存放的是對(duì)應(yīng)的Ranges。
長(zhǎng)按移動(dòng)圖片位置
- (void)p_longPressGestureRecognized:(id)sender {
UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;
UIGestureRecognizerState longPressState = longPress.state;
//手指在tableView中的位置
_moveTableView.fingerLocation = [longPress locationInView:_moveTableView];
//手指按住位置對(duì)應(yīng)的indexPath,可能為nil
_moveTableView.relocatedIndexPath = [_moveTableView indexPathForRowAtPoint:_moveTableView.fingerLocation];
switch (longPressState) {
case UIGestureRecognizerStateBegan:{ //手勢(shì)開(kāi)始,對(duì)被選中cell截圖,隱藏原cell
_moveTableView.originalIndexPath = [_moveTableView indexPathForRowAtPoint:_moveTableView.fingerLocation];
if (_moveTableView.originalIndexPath) {
_moveTableView.firstIndexPath = _moveTableView.originalIndexPath;
[_moveTableView cellSelectedAtIndexPath:_moveTableView.originalIndexPath];
}
break;
}
case UIGestureRecognizerStateChanged:{//點(diǎn)擊位置移動(dòng),判斷手指按住位置是否進(jìn)入其它indexPath范圍,若進(jìn)入則更新數(shù)據(jù)源并移動(dòng)cell
//截圖跟隨手指移動(dòng)
CGPoint center = _moveTableView.snapshot.center;
center.y = _moveTableView.fingerLocation.y;
_moveTableView.snapshot.center = center;
if ([_moveTableView checkIfSnapshotMeetsEdge]) {
[_moveTableView startAutoScrollTimer];
}else{
[_moveTableView stopAutoScrollTimer];
}
//手指按住位置對(duì)應(yīng)的indexPath,可能為nil
_moveTableView.relocatedIndexPath = [_moveTableView indexPathForRowAtPoint:_moveTableView.fingerLocation];
if (_moveTableView.relocatedIndexPath && ![_moveTableView.relocatedIndexPath isEqual:_moveTableView.originalIndexPath]) {
[_moveTableView cellRelocatedToNewIndexPath:_moveTableView.relocatedIndexPath];
}
break;
}
default: { //長(zhǎng)按手勢(shì)結(jié)束或被取消,移除截圖,顯示cell
[_moveTableView stopAutoScrollTimer];
[_moveTableView didEndDraging];
break;
}
}
}
該方法調(diào)用了RTDragCellTableView當(dāng)中的方法,來(lái)實(shí)現(xiàn)移動(dòng)圖片位置。由于是從TextView轉(zhuǎn)到TableView上進(jìn)行移動(dòng)所以,將長(zhǎng)按手勢(shì)操作放在了外面進(jìn)行。
格式化數(shù)據(jù)
- (NSArray *)p_trimIsMove:(BOOL)isMove;//該方法對(duì)一行只有一個(gè)回車(chē)的數(shù)據(jù)進(jìn)行了trim,如果不需要,可以刪除;
圖片選擇,目前并沒(méi)有去進(jìn)行獲取原圖
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto;
代碼地址:仿豆瓣日記圖文編輯