从UIWebView中显示的URL中删除超链接

时间:2011-10-16 22:38:20

标签: iphone objective-c ios4 uiwebview

我正在创建一个应用程序,其中包含最新公司新闻的部分。基本上,该计划最初是使用ASIHTTPRequest来提取所有HTML,通过它搜索标签,然后将信息拉出来获取新闻标题和描述,然后将其显示为我的应用程序滚动视图中的计划文本。然而,这被证明是一场噩梦,因为该网站没有信息显示方式的模式。

我决定在UIWebView中显示URL,该UI显示最新的公司新闻。我希望这是一个单页面视图,因此我的用户无法导航到该网站的其他方面。是否可以阻止站点超链接显示为链接?我的UIWebView代码如下:

UIView *webNewsView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = webNewsView;    

CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
webFrame.origin.y = 0.0f;
webNews = [[UIWebView alloc] initWithFrame:webFrame];
webNews.backgroundColor = [UIColor clearColor];
webNews.scalesPageToFit = YES;
webNews.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
webNews.delegate = self;
[self.view addSubview: webNews];
[webNews loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://myURLHere"]]];

indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicator.frame = CGRectMake(0.0, 0.0, 25.0, 25.0);
indicator.center = self.view.center;
[self.view addSubview: indicator];

任何帮助将不胜感激。 感谢

编辑:我也尝试添加以下内容;

webNews.dataDetectorTypes = UIDataDetectorTypeNone;

这对链接的检测没有任何影响。

2 个答案:

答案 0 :(得分:3)

好的,为dataDetectorTypes设置UIDataDetectorTypeNone应该阻止webview检测链接,这是正确的。

此外使用

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 

UIWebview委托,您只需为您不希望用户允许的URL返回NO ...

此外,您可以在页面加载完

后插入自定义CSS规则
- (void)webViewDidFinishLoad:(UIWebView *)webView 

委托方法,这样:

[MywebView stringByEvaluatingJavaScriptFromString:@"document.styleSheets[0].addRule(\"a.link\", \"color:#FF0000\")"];

所以基于我提到的线程中的CSS,这应该可行:

[MywebView stringByEvaluatingJavaScriptFromString:@"document.styleSheets[0].addRule(\".active\", \"pointer-events: none;\");document.styleSheets[0].addRule(\".active\", \"cursor: default;\")"];

答案 1 :(得分:1)

您还可以在此处进一步自定义Web视图中的链接,即可编辑要传递到webview中的html字符串。具体来说,您可以将CSS添加到html以确定链接的显示方式(颜色,下划线等)。这是一个代码片段,它接受一个html字符串并使用CSS自定义它:

NSString *HTML = [NSString stringWithFormat:@"<html>\n"
                      "<head>\n"
                      "<style type=\"text/css\">\n"
                      "body {font-family: \"%@\"; font-size: %@; color:#%@; background-color:#FFFFFF; margin: 0; padding: 0; line-height: 1.5; }\n"
                      "a:link {color:#00B0EA; text-decoration: none;}\n"
                      "</style>\n"
                      "</head>\n"
                      "<body><script type=\"text/javascript\">window.onload = function() { window.location.href = \"ready://\" + document.body.offsetHeight; } </script>%@</body>\n"
                      "</html>",
                      font.familyName, @(font.pointSize), colorHexString, htmlBodyString];