我以这种方式在网页视图中加载一个html页面:
NSString *description = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.mypage.html"] encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:pRova baseURL:nil];
我需要删除:
<img src="http://www.mypage/image" align="left" style="padding: 0px 10px 0px 0px; width: 301px; height: 280px;" alt="diaspora-uys" />
从NSString *描述,显示没有图像且只有文本的UIwebView。 我怎样才能做到这一点?
答案 0 :(得分:3)
我这样解决了:
- (NSString *)flattenHTML:(NSString *)html {
NSScanner *theScanner;
NSString *gt =nil;
theScanner = [NSScanner scannerWithString:html];
while ([theScanner isAtEnd] == NO) {
// find start of tag
[theScanner scanUpToString:@"<img" intoString:NULL] ;
// find end of tag
[theScanner scanUpToString:@">" intoString:>] ;
}
html = [html stringByReplacingOccurrencesOfString:[ NSString stringWithFormat:@"%@>", gt] withString:@""];
return html;
}
答案 1 :(得分:0)
1 /将HTML内容加载到字符串
中 +(id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)
2 /删除字符串
中的<img />
标记
3 /在webView上使用loadHTMLString
您还可以通过<img />
执行<img
替换来隐藏图像,而不是在HTML字符串中找到<img style='display:none;'
标记。但它仍将被加载......
答案 2 :(得分:0)
答案 3 :(得分:0)
我修改了代码并且它运行得很好。它只会删除src值。不修改剩余值和图像标记
- (NSString *)flattenHTML:(NSString *)html {
NSScanner *theScanner;
NSString *gt = nil;
NSString *temp = nil;
theScanner = [NSScanner scannerWithString:html];
while ([theScanner isAtEnd] == NO) {
// find start of tag
[theScanner scanUpToString:@"<img" intoString:&temp];
//find the src tag
[theScanner scanUpToString:@"src" intoString:&temp];
[theScanner scanUpToString:@"=" intoString:&temp];
[theScanner scanUpToString:@" " intoString:>];
if (!gt) {
[theScanner scanUpToString:@">" intoString:>];
}
if (gt) {
html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@",gt] withString:@"=\"\""];
}
}
return html;
}