Vla*_*mir 96 iphone objective-c uitextview datadetectortypes
当用户在UITextView中触摸自动检测的手机链接时,是否可以执行自定义操作.请不要建议使用UIWebView.
请不要只重复苹果课程中的文字参考 - 当然我已经读过了.
谢谢.
fsa*_*int 139
更新:从ios10,
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction;
Run Code Online (Sandbox Code Playgroud)
从ios7和Later UITextView有委托方法:
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange *NS_DEPRECATED_IOS(7_0, 10_0, "Use textView:shouldInteractWithURL:inRange:forInteractionType: instead");*
Run Code Online (Sandbox Code Playgroud)
拦截点击链接.这是最好的方法.
对于ios6和更早版本,一个很好的方法是通过子类化UIApplication和覆盖-(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
Run Code Online (Sandbox Code Playgroud)
您需要openURL:在您的代理中实施.
现在,要让应用程序从您的新子类开始,请UIApplication在项目中找到main.m文件.在这个引导你的应用程序的小文件中,通常有这一行:
int retVal = UIApplicationMain(argc, argv, nil, nil);
Run Code Online (Sandbox Code Playgroud)
第三个参数是应用程序的类名.所以,将此行替换为:
int retVal = UIApplicationMain(argc, argv, @"MyApplication", nil);
Run Code Online (Sandbox Code Playgroud)
这对我有用.
Raj*_*ana 50
在iOS 7或更高版本中
您可以使用以下UITextView委托方法:
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
Run Code Online (Sandbox Code Playgroud)
如果用户点击或长按URL链接,则文本视图会调用此方法.此方法的实现是可选的.默认情况下,文本视图打开负责处理URL类型的应用程序并将URL传递给它.您可以使用此方法触发替代操作,例如在当前应用程序的Web视图中的URL处显示Web内容.
重要:
仅当文本视图可选但不可编辑时,文本视图中的链接才是交互式的.也就是说,如果UITextView的值为selectable属性为YES且isEditable属性为NO.
在 Swift 5 和 iOS 12 中,您可以使用以下三种模式之一来与UITextView.
UITextView的dataDetectorTypes属性。与 a 中的电话号码、网址或地址进行交互的最简单方法UITextView是使用dataDetectorTypes属性。下面的示例代码显示了如何实现它。使用此代码,当用户点击电话号码时,会UIAlertController弹出一个。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let textView = UITextView()
textView.text = "Phone number: +33687654321"
textView.isUserInteractionEnabled = true
textView.isEditable = false
textView.isSelectable = true
textView.dataDetectorTypes = [.phoneNumber]
textView.isScrollEnabled = false
textView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(textView)
textView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
textView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
textView.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor).isActive = true
}
}
Run Code Online (Sandbox Code Playgroud)
UITextViewDelegate的textView(_:shouldInteractWith:in:interaction:)方法如果您想执行一些自定义操作而不是UIAlertController在使用时点击电话号码时弹出窗口dataDetectorTypes,则必须使您UIViewController符合UITextViewDelegate协议并实施textView(_:shouldInteractWith:in:interaction:)。下面的代码展示了如何实现它:
import UIKit
class ViewController: UIViewController, UITextViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let textView = UITextView()
textView.delegate = self
textView.text = "Phone number: +33687654321"
textView.isUserInteractionEnabled = true
textView.isEditable = false
textView.isSelectable = true
textView.dataDetectorTypes = [.phoneNumber]
textView.isScrollEnabled = false
textView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(textView)
textView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
textView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
textView.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor).isActive = true
}
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
/* perform your own custom actions here */
print(URL) // prints: "tel:+33687654321"
return false // return true if you also want UIAlertController to pop up
}
}
Run Code Online (Sandbox Code Playgroud)
NSAttributedString和NSAttributedString.Key.link作为替代方案,您可以使用NSAttributedString并URL为其NSAttributedString.Key.link属性设置 a 。下面的示例代码显示了它的可能实现。使用此代码,当用户点击属性字符串时,会UIAlertController弹出一个。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let attributedString = NSMutableAttributedString(string: "Contact: ")
let phoneUrl = NSURL(string: "tel:+33687654321")! // "telprompt://+33687654321" also works
let attributes = [NSAttributedString.Key.link: phoneUrl]
let phoneAttributedString = NSAttributedString(string: "phone number", attributes: attributes)
attributedString.append(phoneAttributedString)
let textView = UITextView()
textView.attributedText = attributedString
textView.isUserInteractionEnabled = true
textView.isEditable = false
textView.isSelectable = true
textView.isScrollEnabled = false
textView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(textView)
textView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
textView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
textView.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor).isActive = true
}
}
Run Code Online (Sandbox Code Playgroud)
迅捷版:
您的标准UITextView设置应如下所示,不要忘记委托和dataDetectorTypes。
var textView = UITextView(x: 10, y: 10, width: CardWidth - 20, height: placeholderHeight) //This is my custom initializer
textView.text = "dsfadsaf www.google.com"
textView.selectable = true
textView.dataDetectorTypes = UIDataDetectorTypes.Link
textView.delegate = self
addSubview(textView)
Run Code Online (Sandbox Code Playgroud)
课程结束后,添加以下内容:
class myVC: UIViewController {
//viewdidload and other stuff here
}
extension MainCard: UITextViewDelegate {
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
//Do your stuff over here
var webViewController = SVModalWebViewController(URL: URL)
view.presentViewController(webViewController, animated: true, completion: nil)
return false
}
}
Run Code Online (Sandbox Code Playgroud)
对于Swift 3
textView.delegate = self
extension MyTextView: UITextViewDelegate
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
GCITracking.sharedInstance.track(externalLink: URL)
return true
}
}
Run Code Online (Sandbox Code Playgroud)
或者目标是> = IOS 10
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
56723 次 |
| 最近记录: |