iOS技术iOS开发

iOS富文本编辑器--高仿石墨文档编辑图文文章

2018-01-02  本文已影响104人  Shin_R
实现再次编辑功能笔记。借鉴下面demo进行修改。(demo转载自https://github.com/littleMeaning/SimpleWord

gitHub地址:GitHub - littleMeaning/SimpleWord: 模仿石墨文档文档编辑器,使用原生代码写的富文本编辑器,支持字体、颜色设置,支持缩进,列表、checkbox,支持插入图片等。 (*开发中)

- (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) {
        
        //过去数组中的标签
        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添加到图片数组中
        [imageurlArray addObject:imagekUrl];
    }
    
    return imageurlArray;
}
-(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];
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读