UITextView中的超链接

我正在尝试使用超链接创建UITextView,以便当用户单击链接时,他们将被带到safari以打开网页。 我已阅读文本视图的链接检测器,但如果文本中存在实际url(即www.google.com),这些示例始终显示链接检测有效。 我希望它是常规文本,单击时会打开关联的URL。 (即Google就是文字,点击后会打开一个urlwww.google.com)。 如何在iOS7 / 8中完成此操作?

使用NSAttributedString

 NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"Google" attributes:@{ NSLinkAttributeName: [NSURL URLWithString:@"http://www.google.com"] }]; self.textView.attributedText = attributedString; 

当然,您可以只将文本的一部分设置为链接。 请在此处阅读有关NSAttributedString更多信息。

如果您想在打开链接之前获得更多控制权并执行某些操作。 您可以将委托设置为UITextView

 - (void)viewDidLoad { ... self.textView.delegate = self; // self must conform to UITextViewDelegate protocol } ... - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange { // Do whatever you want here NSLog(@"%@", URL); // URL is an instance of NSURL of the tapped link return YES; // Return NO if you don't want iOS to open the link } 

Swift 3,iOS10,Xcode 9

@ sikhapol的答案真的很好,如果你确切地知道你要解析的单词,如“单词字典”一些如何

它完全是关于UITextView中显示的字符串

我的解决方案是基于文本渲染,如果你使UITextView渲染一个html标签,你可以使用href标签

以下是您可能使用的一些代码参考

首先,您需要从界面构建器或表单代码配置UITextView

  1. 可选
  2. 数据检测

注意:不要使textview可编辑

界面构建器

在此处输入图像描述

编程

  let htmlData = NSString(string: "go to google and search for it").data(using: String.Encoding.unicode.rawValue) let attributedString = try! NSAttributedString(data: htmlData!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) yourUIViewView.isSelectable = true yourUIViewView.dataDetectorTypes = .link yourUIViewView.attributedText = attributedString yourUIViewView.delegate = self 

对于UITextViewDelegate

  func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool { // check for the url string for performing your own custom actions here let urlString = URL.absoluteString // Return NO if you don't want iOS to open the link return true } 

如果您想在UITextView中使用活动子字符串,那么您可以使用我的扩展TextView …简短而简单。 您可以根据需要进行编辑。

如何使用(range = substring location):

 [self.textView addTapActionWithRange:range withActionBlock:^{ // anything you want to do - show something }]; 

结果: 在此处输入图像描述

源代码: https : //github.com/marekmand/ActiveSubstringTextView

此代码示例在同一标签中有两个不同的链接,并设置URL颜色以避免默认的蓝色。

 UITextView * textTerm = [UITextView new]; NSMutableAttributedString *attrRight = [[NSMutableAttributedString alloc] initWithString:@"Terms of Service" attributes:@{ NSLinkAttributeName: [NSURL URLWithString:@"http://www.google.com"] }]; NSMutableAttributedString *attrLeft = [[NSMutableAttributedString alloc] initWithString:@"Privacy Policy" attributes:@{ NSLinkAttributeName: [NSURL URLWithString:@"http://www.google.com"] }]; [attrRight appendAttributedString:attrLeft]; textTerm.attributedText = attrRight; textTerm.editable = NO; textTerm.dataDetectorTypes = UIDataDetectorTypeAll; textTerm.linkTextAttributes = [UIColor whiteColor]; textTerm.backgroundColor = [UIColor clearColor]; 
Interesting Posts