實現(xiàn):1侵歇、首次圖文混編碍彭,輸出是html標簽的字符串;
????????????2摸袁、將html標簽的內(nèi)容再次編輯钥顽;
創(chuàng)建編輯控制器
.h文件
@property (nonatomic,strong)NSString *inHtmlString;
block是將編輯的內(nèi)容返回上個頁面操作
@property (nonatomic, copy)void (^postBlock)(NSString *htmlString);
.m文件
JavaScriptCore/JavaScriptCore.h
WebKit/WebKit.h
實現(xiàn)WKWebView的代理及相冊的代理(懶得弄。用的系統(tǒng)相冊)
NSString*_htmlString;//保存輸出的富文本
? ? NSMutableArray*_imageArr;//保存添加的圖片
@property (nonatomic, strong) WKWebView *webView;
加載寫好的 .html文件 給個簡易版本的即可
NSBundle *bundle = [NSBundle mainBundle];
? ? NSURL*indexFileURL = [bundleURLForResource:@"richTextEditor"withExtension:@"html"];
? ? [self.webView loadRequest:[NSURLRequest requestWithURL:indexFileURL]];
創(chuàng)建一個button 選擇圖片靠汁,給個點擊事件
[btn1 addTarget:self action:@selector(addImage) forControlEvents:UIControlEventTouchUpInside];
創(chuàng)建一個button 保存html字符串蜂大,給個點擊事件
[btn2 addTarget:self action:@selector(printHTML) forControlEvents:UIControlEventTouchUpInside];
- (void)printHTML{
? ? [self.webView evaluateJavaScript:@"document.getElementById('content').innerHTML" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
? ? ? ? NSLog(@"html=%@",result);
? ? ? ? !self.postBlock?:self.postBlock(result);
? ? ? ? [self.navigationController popViewControllerAnimated:YES];
? ? }];
}
- (void)addImage{
? ? UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
? ? imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
? ? imagePickerController.delegate=self;
? ? [self presentViewController:imagePickerController animated:YES completion:nil];
}
實現(xiàn)相冊代理方法
#pragma mark - ImagePickerController Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
? ? UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
? ? NSData *data = UIImagePNGRepresentation(image);
? ? if(2*1024*1024>[datalength]) {
? ? ? ? data =UIImageJPEGRepresentation(image,0.6);
? ? }else{
? ? }
? ? UIImage*resultImage = [UIImageimageWithData:data];
? ? NSString *imagePath = [NSString stringWithFormat:@"%@", [info objectForKey:UIImagePickerControllerImageURL]];//本地圖片路徑
? ? [datawriteToFile:imagePathatomically:YES];
此處是將已添加的圖片上傳至你們的服務器
上傳成功后執(zhí)行
? ? ? ? ? ? NSString*url =服務器圖片地址;
? ? ? ? ? ? NSString*script = [NSStringstringWithFormat:@"window.insertImage('%@', '%@')", imagePath, url];
? ? ? ? ? ? NSDictionary*dic =@{@"url":url,@"image":image,@"name":imagePath};
? ? ? ? ? ? [_imageArraddObject:dic];
? ? ? ? ? ? [self.webViewevaluateJavaScript:scriptcompletionHandler:^(id_Nullableresult,NSError*_Nullableerror) {
? ? ? ? ? ? }];
? ? ? ? ? ? [self dismissViewControllerAnimated:YES completion:nil];
}