UIWebView中的图文混排
发布日期:2022-03-18 08:27:44
浏览次数:39
分类:技术文章
本文共 3344 字,大约阅读时间需要 11 分钟。
代码来自于开源项目。是一个模仿网易新闻的客户端。
其拼接HTML的原理如下:
#pragma mark - ******************** 拼接html语言- (void)showInWebView{ NSMutableString *html = [NSMutableString string]; [html appendString:@""]; [html appendString:@""]; [html appendFormat:@" ",[[NSBundle mainBundle] URLForResource:@"SXDetails.css" withExtension:nil]]; [html appendString:@""]; [html appendString:@""]; [html appendString:[self touchBody]]; [html appendString:@""]; [html appendString:@""]; [self.webView loadHTMLString:html baseURL:nil];}//正文内容- (NSString *)touchBody{ NSMutableString *body = [NSMutableString string]; [body appendFormat:@"%@",self.detailModel.title];//标题 [body appendFormat:@"%@",self.detailModel.ptime];//时间 if (self.detailModel.body != nil) { [body appendString:self.detailModel.body]; } // 遍历img for (SXDetailImgModel *detailImgModel in self.detailModel.img) { NSMutableString *imgHtml = [NSMutableString string]; // 设置img的div [imgHtml appendString:@""]; // 数组存放被切割的像素 图片像素 NSArray *pixel = [detailImgModel.pixel componentsSeparatedByString:@"*"]; CGFloat width = [[pixel firstObject]floatValue]; CGFloat height = [[pixel lastObject]floatValue]; // 判断是否超过最大宽度 CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width * 0.96; if (width > maxWidth) { height = maxWidth / width * height; width = maxWidth; } NSString *onload = @"this.onclick = function() {" " window.location.href = 'sx:src=' +this.src;" "};"; [imgHtml appendFormat:@" ",onload,width,height,detailImgModel.src]; // 结束标记 [imgHtml appendString:@""]; // 替换标记 [body replaceOccurrencesOfString:detailImgModel.ref withString:imgHtml options:NSCaseInsensitiveSearch range:NSMakeRange(0, body.length)]; } return body;}
在点击网页中的图片时,会提示保存:
#pragma mark - ******************** 将发出通知时调用- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ NSString *url = request.URL.absoluteString; NSRange range = [url rangeOfString:@"sx:src="]; if (range.location != NSNotFound) { NSInteger begin = range.location + range.length; NSString *src = [url substringFromIndex:begin]; [self savePictureToAlbum:src]; return NO; } return YES;}#pragma mark - ******************** 保存到相册方法- (void)savePictureToAlbum:(NSString *)src{ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要保存到相册吗?" preferredStyle:UIAlertControllerStyleActionSheet]; [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]]; [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { NSURLCache *cache =[NSURLCache sharedURLCache]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:src]]; NSData *imgData = [cache cachedResponseForRequest:request].data; UIImage *image = [UIImage imageWithData:imgData]; UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); }]]; [self presentViewController:alert animated:YES completion:nil];}
转载地址:https://windzen.blog.csdn.net/article/details/53610332 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
网站不错 人气很旺了 加油
[***.192.178.218]2024年03月31日 06时26分47秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
【重复制造精讲】REM Pull List 拉料单简介
2019-04-28
SAP change document T-code SCDO(SAP更改字段级记录)
2019-04-28
CSDN博客的安全性为什么一直如此薄弱?
2019-04-28
【BOM精讲】BOM 入门基本常识
2019-04-28
【术语扫盲】CKD (全散件组装)
2019-04-28
【术语扫盲】SKD 半散装件
2019-04-28
【PP生产订单】收货Goods Receipt
2019-04-28
【转】从人类简史到今日简史,简史三部曲,尤瓦尔赫拉利的进阶之路
2019-04-28
既是客户又是供应商清帐配置
2019-04-28
SAP财务清账原理与方式
2019-04-28
SAP-PM 的工具管理篇
2019-04-28
PM后台配置路径
2019-04-28
一、【SAP-PM模块】SAP系统PM模块概述
2019-04-28
二、【SAP-PM模块】PM模块(含服务采购)组织架构
2019-04-28
三、【SAP-PM模块】PM模块主数据
2019-04-28
四、【SAP-PM模块】维护业务处理流程
2019-04-28
五、【SAP-PM模块】服务采购业务流程
2019-04-28
六、【SAP-PM模块】预防性维护业务流程
2019-04-28
七、【SAP-PM模块】信息系统 & 报表分析
2019-04-28
SAP订单返工
2019-04-28