2011-10-16 57 views
0

我正在製作一個應用程序,其中包含最新的公司新聞部分。基本上這個計劃最初是使用ASIHTTPRequest來提取所有的HTML,通過它搜索標籤,然後將信息提取出來用於新聞標題和描述,然後在我的應用程序的滾動視圖中顯示它作爲計劃文本。然而,這被證明是一場噩夢,因爲該網站沒有模式顯示信息。從UIWebView中顯示的URL中刪除超鏈接

我已經決定在顯示最新公司新聞的UIWebView中顯示URL。我希望這是一個頁面視圖,因此我的用戶無法導航到網站的其他方面。是否有可能停止顯示爲鏈接的網站超鏈接?我的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; 

這對鏈接的檢測沒有任何區別。

回答

3

好的,爲dataDetectorTypes設置UIDataDetectorTypeNone應該可以防止webview檢測鏈接,這是正確的。

而且使用

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

UIWebView的委託,你可以簡單地返回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;\")"]; 
+0

設置UIDataDetectorTypeNone並不奇怪。所有的鏈接仍然是可點擊的。即使通過界面生成器取消選中框也沒有區別。今晚我會嘗試你的最後一條建議,謝謝。 –

+0

您肯定可以禁用鏈接,使其可以通過shouldStartLoadWithRequest委託進行點擊。至於禁用鏈接顯示爲鏈接,則基於上面示例中顯示的方式使用正確的CSS規則。也許有一個更好的CSS方法,但是我沒有那麼好在CSS – Lefteris

+0

我已經實現了[webNews stringByEvaluatingJavaScriptFromString:@「document.styleSheets [0] .addRule(\」a.link \「,\」color:#FF0000 \ 「)」];在 - (void)webViewDidFinishLoad:(UIWebView *)webView委託方法中,它沒有區別。所有鏈接仍然可點擊 –

1

有一件事要進一步自定義您的鏈接在網頁視圖編輯HTML字符串你進入webview。具體來說,您可以將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];