如何拦截点击UITextView中的链接? [英] How to intercept click on link in 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.
谢谢。
推荐答案
更新:来自 ios10 ,
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction;
来自 ios7 及更高版本 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.
对于 ios6 和更早的一个很好的方法是通过继承 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:
。
现在,要让应用程序以 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屋!