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

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

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年03月31日 06时26分47秒