从 UIWebView 中显示的 URL 中删除超链接
我正在制作一个应用程序,其中包含最新的公司新闻部分。基本上,最初的计划是使用 ASIHTTPRequest 提取所有 HTML,搜索标签,然后提取新闻标题和描述信息,然后在我的应用程序的滚动视图中将其显示为计划文本。然而,这被证明是一场噩梦,因为该网站没有信息显示方式的模式。
此后我决定在 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;
这对于链接的检测没有影响。
I am making an app which has a section for latest company news. Basically the plan was initially to use ASIHTTPRequest to extract all the HTML, search through it for tags then pull the information out for news title and description then display it as just plan text in a scroll view of my app. However, this was proving to be a nightmare as the site has no pattern to the way the information is displayed.
I have since decided to display the URL in a UIWebView which shows the latest company news. I want this to be a one page view so my user cannot navigate to other aspects of the site. Is it possible to stop the sites hyperlinks showing up as links? My code for the UIWebView is as follows;
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];
Any help would be appreciated.
Thanks
EDIT: I have also tried adding the following;
webNews.dataDetectorTypes = UIDataDetectorTypeNone;
Which makes no difference to the detection of links.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,为 dataDetectorTypes 设置 UIDataDetectorTypeNone ,应该可以防止 webview 检测链接,这是正确的。
此外,使用
UIWebview 委托,您可以简单地为您不希望用户允许遵循的 URL 返回 NO...
此外,您可以在页面完成加载
委托方法后插入自定义 CSS 规则,如下所示:
因此,根据我提到的线程中的 CSS,这应该有效:
OK, setting the UIDataDetectorTypeNone for dataDetectorTypes, should prevent the webview from detecting the links, this is correct.
Furthermore using the
UIWebview delegate, you can simply return NO for the URL's you don't want the user to allow following...
Furthermore, you can insert custom CSS Rules, after the page has finished loading in the
delegate method, this way:
So based on the CSS from the thread I mentioned, this should work:
您还可以在此处进一步自定义 Web 视图中的链接的一件事是编辑您传递到 Web 视图中的 html 字符串。具体来说,您可以向 html 添加 CSS 来确定链接的显示方式(颜色、下划线等)。下面是一个代码片段,它采用 html 字符串并使用 CSS 对其进行自定义:
One thing you can also do here to further customize your links in the web view is edit the html string you are passing into the webview. Specifically, you can add CSS to the html to determine how links will be shown (colors, underlines, etc). Here is a code snippet which takes an html string and customizes it with CSS: