UIWebView中的图文混排
发布日期:2022-03-18 08:27:44 浏览次数:38 分类:技术文章

本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:有视差的滚动视图-Parallax ScrollView In Swift
下一篇:UIScrollView下拉模糊效果

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月07日 11时54分08秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

Leetcode 121. 买卖股票的最佳时机(DAY 26) ---- 动态规划学习期 2019-04-27
Leetcode 746. 使用最小花费爬楼梯(DAY 26) ---- 动态规划学习期 2019-04-27
Leetcode 面试题 17.16. 按摩师(DAY 26) ---- 动态规划学习期 2019-04-27
Leetcode 70. 爬楼梯(DAY 26) ---- 动态规划学习期 2019-04-27
Leetcode 392. 判断子序列(DAY 26)---- 动态规划学习期 双百解法 2019-04-27
Leetcode 面试题 08.01. 三步问题(DAY 26) ---- 动态规划学习期 2019-04-27
Leetcode 877. 石子游戏(DAY 27) ---- 动态规划学习期 2019-04-27
Leetcode 714. 买卖股票的最佳时机含手续费(DAY 27) ---- 动态规划学习期 2019-04-27
Leetcode 96. 不同的二叉搜索树(DAY 28) ---- 动态规划学习期 (含题解) 2019-04-27
Leetcode 剑指 Offer 47. 礼物的最大价值(DAY 28) ---- 动态规划学习期 2019-04-27
Leetcode 120. 三角形最小路径和(DAY 28) ---- 动态规划学习期 2019-04-27
Leetcode 1227. 飞机座位分配概率(DAY 29) ---- 动态规划学习期 (成功留校) 2019-04-27
Leetcode 712. 两个字符串的最小ASCII删除和(DAY 30)---- 动态规划好难 学习期(懒狗复工) 2019-04-27
Leetcode 62. 不同路径(DAY 31) ---- 动态规划学习期 2019-04-27
Leetcode 983. 最低票价(DAY 31) ---- 动态规划学习期 2019-04-27
Python课后作业 2. 逆序排列(第三次作业) 2019-04-27
Python课后作业 3. 字符串去重排序 ---- (第三次作业) 2019-04-27
Python课后作业 4. 绩点计算 ---- (第三次作业) 2019-04-27
C++面向对象程序设计 031:山寨版istream_iterator ---- (北大Mooc) 2019-04-27
C++面向对象程序设计 032:这个模板并不难 ---- (北大Mooc) 2019-04-27