代码来自于开源项目SXNews。SXNews是一个模仿网易新闻的客户端。
其拼接HTML的原理如下:
#pragma mark - ******************** 拼接html语言
- (void)showInWebView
{
NSMutableString *html = [NSMutableString string];
[html appendString:@"<html>"];
[html appendString:@"<head>"];
[html appendFormat:@"<link rel=\"stylesheet\" href=\"%@\">",[[NSBundle mainBundle] URLForResource:@"SXDetails.css" withExtension:nil]];
[html appendString:@"</head>"];
[html appendString:@"<body>"];
[html appendString:[self touchBody]];
[html appendString:@"</body>"];
[html appendString:@"</html>"];
[self.webView loadHTMLString:html baseURL:nil];
}
//正文内容
- (NSString *)touchBody
{
NSMutableString *body = [NSMutableString string];
[body appendFormat:@"<div class=\"title\">%@</div>",self.detailModel.title];//标题
[body appendFormat:@"<div class=\"time\">%@</div>",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:@"<div class=\"img-parent\">"];
// 数组存放被切割的像素 图片像素
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:@"<img onload=\"%@\" width=\"%f\" height=\"%f\" src=\"%@\">",onload,width,height,detailImgModel.src];
// 结束标记
[imgHtml appendString:@"</div>"];
// 替换标记
[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];
}