如何拦截点击 UITextView 中的链接? [英] How to intercept click on link in UITextView?

查看:30
本文介绍了如何拦截点击 UITextView 中的链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在用户触摸 UITextView 中自动检测到的电话链接时执行自定义操作.请不要建议改用 UIWebView.

Is it possible to perform custom action when user touch autodetected phone link in UITextView. Please do not advice to use UIWebView instead.

并且请不要只是重复苹果类参考中的文本- 当然我已经读过了.

And please don't just repeat text from apple classes reference - certainly I've already read it.

谢谢.

推荐答案

更新:来自 ,

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction;

来自 和更高版本 UITextView 有委托方法:

From ios7 and Later UITextView has the delegate method:

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange *NS_DEPRECATED_IOS(7_0, 10_0, "Use textView:shouldInteractWithURL:inRange:forInteractionType: instead");*

拦截对链接的点击.这是最好的方法.

to intercept the clicks to links. And this is the best way to do it.

对于 和更早版本的一种很好的方式这样做是通过子类化 UIApplication 并覆盖 -(BOOL)openURL:(NSURL *)url

For ios6 and earlier a nice way to do this is to by subclassing UIApplication and overwriting the -(BOOL)openURL:(NSURL *)url

@interface MyApplication : UIApplication {

}

@end

@implementation MyApplication


-(BOOL)openURL:(NSURL *)url{
    if  ([self.delegate openURL:url])
         return YES;
    else
         return [super openURL:url];
}
@end

您需要在您的委托中实现 openURL:.

You will need to implement openURL: in your delegate.

现在,要让应用程序从 UIApplication 的新子类开始,请在您的项目中找到文件 main.m.在引导您的应用程序的这个小文件中,通常有这样一行:

Now, to have the application start with your new subclass of UIApplication, locate the file main.m in your project. In this small file that bootstraps your app, there is usually this line:

int retVal = UIApplicationMain(argc, argv, nil, nil);

第三个参数是应用程序的类名.因此,将此行替换为:

The third parameter is the class name for your application. So, replacing this line for:

int retVal = UIApplicationMain(argc, argv, @"MyApplication", nil);

这对我有用.

这篇关于如何拦截点击 UITextView 中的链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆