gitHub地址:GitHub - littleMeaning/SimpleWord: 模仿石墨文檔文檔編輯器妙蔗,使用原生代碼寫的富文本編輯器呆抑,支持字體宿刮、顏色設(shè)置互站,支持縮進(jìn),列表僵缺、checkbox胡桃,支持插入圖片等。 (*開發(fā)中)
- 在LMWordViewController中添加對象方法磕潮,傳入編輯的html數(shù)據(jù)翠胰。
- 獲取html數(shù)據(jù)中的圖片
- (NSArray *) getImageurlFromHtml:(NSString *) webString
{
NSMutableArray * imageurlArray = [NSMutableArray arrayWithCapacity:1];
//標(biāo)簽匹配
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ù)組中的標(biāo)簽
NSRange range = [result range];
NSString * subString = [webString substringWithRange:range];
//從圖片中的標(biāo)簽中提取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];
}
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者