-
(void)showNews:(NSDictionary
*)news
{
NSString
*body = news[@"body"];
NSDictionary
*img = [news[@"img"]
lastObject];
NSString
*imgHTML = [NSString
stringWithFormat:@"<img
src=\"%@\" width=\"300\" height=\"171\">",
img[@"src"]];
UIWebView
*webView = [[UIWebView
alloc] init];
webView.frame
= self.view.bounds;
[self.view
addSubview:webView];
NSString
*html = [body stringByReplacingOccurrencesOfString:img[@"ref"]
withString:imgHTML];
NSString
*title = news[@"title"];
NSString
*time
= news[@"ptime"];
NSString
*header = [NSString
stringWithFormat:@"<div
class=\"title\">%@</div><div class=\"time\">%@</div>",
title, time];
html
= [NSString
stringWithFormat:@"%@%@",
header, html];
NSURL
*cssURL = [[NSBundle
mainBundle] URLForResource:@"news"
withExtension:@"css"];
html
= [NSString
stringWithFormat:@"%@<link
rel=\"stylesheet\" href=\"%@\">",
html, cssURL];
[webView
loadHTMLString:html baseURL:nil];
}
NSString
*url = @"http://ibaby.ipadown.com/api/food/food.show.detail.php?id=";
NSString
*webSite = [url stringByAppendingString:self.Idtext];
NSURL
*siteURL= [NSURL
URLWithString:webSite];
NSURLRequest
*request = [NSURLRequest
requestWithURL:siteURL];
dispatch_async(dispatch_get_global_queue(0,
0), ^{
NSURLSessionDataTask
*dataTask = [[NSURLSession
sharedSession]dataTaskWithRequest:request completionHandler:^(NSData
* _Nullable data, NSURLResponse
* _Nullable response, NSError
* _Nullable error) {
self.htmlAbosluteString
= [[NSString
alloc]initWithData:data encoding:NSUTF8StringEncoding];
self.htmlAbosluteString
= [self.htmlAbosluteString
stringByReplacingOccurrencesOfString:@"<img
width=\"0\" height=\"0\" style=\"width: 803px; height: 121px;\" src=\"http://ubmcmm.baidustatic.com/media/v1/0f0007y3CMO4YsUwoxXbB6.jpg\"></img>"
withString:@""];
self.htmlAbosluteString
= [self.htmlAbosluteString
stringByReplacingOccurrencesOfString:str9 withString:@""];
dispatch_async(dispatch_get_main_queue(),
^{
[self.webView.web
loadHTMLString:self.htmlAbosluteString
baseURL:nil];
});
}];
[dataTask
resume];
CGRect
tmpFrame = self.webView.web.frame;
CGFloat
h = self.navigationController.navigationBar.frame.size.height
+ 20;
tmpFrame.origin.y
-= h;
self.webView.web.frame
= tmpFrame;
});
NSString
*str = @".jpg\"
height=\"200\" width=\"340\"/>";
-
(NSString
*)transformMessage:(NSString
*)message withString:(NSString
*)string
{
NSString
*messageCopy = message;
NSScanner
*theScanner;
NSString
*text = nil;
theScanner
= [NSScanner
scannerWithString:messageCopy];
while
([theScanner isAtEnd] == NO)
{
[theScanner
scanUpToString:@".jpg"
intoString:NULL]
;
if
([theScanner scanUpToString:@">"
intoString:&text] || [theScanner scanUpToString:@"/>"
intoString:&text]) {
if
(text != nil)
{
messageCopy
= [messageCopy stringByReplacingOccurrencesOfString:[NSString
stringWithFormat:@"%@>",
text] withString:string];
NSLog(@"%@",messageCopy);
break;
}
}
}
return
messageCopy;
}
作用是把start
tag和end tag 之间替换成string的值