iOS富文本編輯器--高仿石墨文檔編輯圖文文章

實現(xiàn)再次編輯功能筆記。借鑒下面demo進行修改。(demo轉(zhuǎn)載自https://github.com/littleMeaning/SimpleWord

gitHub地址:GitHub - littleMeaning/SimpleWord: 模仿石墨文檔文檔編輯器,使用原生代碼寫的富文本編輯器,支持字體、顏色設置,支持縮進,列表、checkbox,支持插入圖片等。 (*開發(fā)中)

  • 在LMWordViewController中添加對象方法,傳入編輯的html數(shù)據(jù)。
  • 獲取html數(shù)據(jù)中的圖片
- (NSArray *) getImageurlFromHtml:(NSString *) webString
{
    NSMutableArray * imageurlArray = [NSMutableArray arrayWithCapacity:1];
    
    //標簽匹配
    NSString *parten = @"<img(.*?)>";
    NSError* error = NULL;
    NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:parten options:0 error:&error];
    
    NSArray* match = [reg matchesInString:webString options:0 range:NSMakeRange(0, [webString length] - 1)];
    
    for (NSTextCheckingResult * result in match) {
        
        //過去數(shù)組中的標簽
        NSRange range = [result range];
        NSString * subString = [webString substringWithRange:range];
        
        
        //從圖片中的標簽中提取ImageURL
        NSRegularExpression *subReg = [NSRegularExpression regularExpressionWithPattern:@"http://(.*?)\"" options:0 error:NULL];
        NSArray* match = [subReg matchesInString:subString options:0 range:NSMakeRange(0, [subString length] - 1)];
        NSTextCheckingResult * subRes = match[0];
        NSRange subRange = [subRes range];
        subRange.length = subRange.length -1;
        NSString * imagekUrl = [subString substringWithRange:subRange];
        
        //將提取出的圖片URL添加到圖片數(shù)組中
        [imageurlArray addObject:imagekUrl];
    }
    
    return imageurlArray;
}
  • 用把獲取到的圖片URL數(shù)組 替換一個個圖片地址的null值
-(void)handlerAllAttechmentWith:(NSAttributedString *)attributedString withimagesArr:(NSArray*)imageUrl;
{
    NSMutableArray *attachmentArr=[NSMutableArray array];
    NSRange effectiveRange = NSMakeRange(0, 0);
    while (effectiveRange.location + effectiveRange.length < attributedString.length) {
        NSDictionary *attributes = [attributedString attributesAtIndex:effectiveRange.location effectiveRange:&effectiveRange];
        NSTextAttachment *attachment = attributes[@"NSAttachment"];
        if (attachment) {
            [attachmentArr addObject:attachment];
        }
        effectiveRange = NSMakeRange(effectiveRange.location + effectiveRange.length, 0);
    }
    if (attachmentArr.count == imageUrl.count) {
    
        for (int i=0; i<attachmentArr.count; i++) {
    
            NSTextAttachment *att=attachmentArr[i];
            att.attachmentType = LMTextAttachmentTypeImage;
            att.userInfo=imageUrl[i];
        }
    }
}
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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