如何拦截点击 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:
.
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屋!