根据属性,创建NSAttributedString
例如:// [标签名称_粗细_字号_字体颜色_下划线_斜体_背景色_对其方式]
[custom_0_16_#004455_0_0_#000000_01]我是内容[/custom]
二、NSAttributedString 转 自定义标签
获得NSAttributedString数据
使用对应的方法,获取NSAttributedString中字符串的属性信息
根据属性信息,定义自己的自定义标签
例如:// 获取富文本中,每段字符串的属性信息
实现: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]
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)