仿豆瓣日記圖文編輯

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

圖片編輯器.gif

初始化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;

代碼地址:仿豆瓣日記圖文編輯

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 乙:乙木雖柔,刲羊解牛。懷丁抱丙,跨鳳乘猴。虛濕之地,騎馬亦憂(yōu)。藤蘿系甲,可春可秋。 坐丑未能制柔土,只要有一丙丁...
    海靈島主閱讀 334評(píng)論 0 1
  • “笑笑,你說(shuō)喜歡一個(gè)人是什么感覺(jué)?。俊鼻щx茫然,好奇的問(wèn)道。 “這個(gè)嘛,怎么說(shuō)呢,首先看不見(jiàn)他的時(shí)候,你會(huì)想他,真...
    白梁開(kāi)閱讀 348評(píng)論 0 1
  • 今天看到了簡(jiǎn)書(shū)的年終報(bào)告,差一點(diǎn)我就完成了一本【悲慘世界】的內(nèi)容。 有時(shí)我挺渴望別人看到我的文章,給我點(diǎn)贊,即使只...
    遇見(jiàn)龍一閱讀 156評(píng)論 0 1
  • 「溫暖」 「逗趣」 小視頻·艾瑪風(fēng)油精啊 「雜燴」 小視頻·食·水果冰品 「FR April」 歌·男孩別哭 風(fēng)雖...
    予緗閱讀 274評(píng)論 0 0

友情鏈接更多精彩內(nèi)容