iOS富文本编辑器

作者 : 开心源码 本文共2434个字,预计阅读时间需要7分钟 发布时间: 2022-05-14 共206人阅读

实现:1、初次图文混编,输出是html标签的字符串;

            2、将html标签的内容再次编辑;

创立编辑控制器

.h文件

@property (nonatomic,strong)NSString *inHtmlString;

block是将编辑的内容返回上个页面操作

@property (nonatomic, copy)void (^postBlock)(NSString *htmlString);

.m文件

JavaScriptCore/JavaScriptCore.h

WebKit/WebKit.h

实现WKWebView的代理商及相册的代理商(懒得弄。用的系统相册)

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]];

创立一个button 选择图片,给个点击事件

[btn1 addTarget:self action:@selector(addImage) forControlEvents:UIControlEventTouchUpInside];

创立一个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];

}

实现相册代理商方法

#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];

此处是将已增加的图片上传至你们的服务器

上传成功后执行

            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];

}

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » iOS富文本编辑器

发表回复