UITextView中的超链接

Joh*_*aum 15 objective-c uitextview ios ios8

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

yus*_*024 21

使用 NSAttributedString

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

当然,您可以只将文本的一部分设置为链接.请详细了解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
}
Run Code Online (Sandbox Code Playgroud)


Amr*_*gry 13

Swift 3,iOS10,Xcode 9

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

它是关于UITextView中显示的字符串的全部内容

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

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

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

  1. 可选
  2. 数据检测

注意:不要使textview可编辑

界面构建器

在此输入图像描述

编程

         let htmlData = NSString(string: "go to <a href=\"http://www.google.com\">google</a> 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
Run Code Online (Sandbox Code Playgroud)

对于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
     }
Run Code Online (Sandbox Code Playgroud)

  • 这是一种更新的方法:`let attributedString = try?NSAttributedString(data:Data(“ some &lt;em&gt; cool &lt;/ em&gt; text” .utf8),选项:[.documentType:NSAttributedString.DocumentType.html],documentAttributes:无) (3认同)

spl*_*ngi 6

我编写和使用的一个漂亮的小扩展(Swift 4.2,在iOS 12.1上测试)

extension NSAttributedString {
    func replace(placeholder: String, with hyperlink: String, url: String) -> NSAttributedString {
        let mutableAttr = NSMutableAttributedString(attributedString: self)

        let hyperlinkAttr = NSAttributedString(string: hyperlink, attributes: [NSAttributedString.Key.link: URL(string: url)!])

        let placeholderRange = (self.string as NSString).range(of: placeholder)

        mutableAttr.replaceCharacters(in: placeholderRange, with: hyperlinkAttr)
        return mutableAttr
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

//Set through code or through interface builder
footerText.isSelectable = true
footerText.dataDetectorTypes = .link

//Keeps the original formatting from xib or storyboard
footerText.text = "By continuing, you are indicating that you accept our @Terms@ and @Privacy@."
footerText.attributedText = footerText.attributedText?
        .replace(placeholder: "@Terms@", with: "Terms and Conditions", url: AppUrl.terms)
        .replace(placeholder: "@Privacy@", with: "Privacy Policy", url: AppUrl.privacy)
Run Code Online (Sandbox Code Playgroud)