ios - UITextView href 链接不起作用

标签 ios swift uitextview nsattributedstring

这是要显示的文本:

© Gregory A. Dunbar <a href=\"http://gregoryadunbar.com\" rel=\"nofollow\">gregoryadunbar.com</a>\n  

这是我用来显示 UITextView 和触发链接的代码:

这里的p.caption就是上面的字符串

textViewCaption = UITextView(frame: CGRect(x: labelPadding, y: 0.0, width: self.bounds.size.width - labelPadding * 2.0, height: self.bounds.size.height))
        textViewCaption.delegate = self
        textViewCaption.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        textViewCaption.isOpaque = false
        textViewCaption.backgroundColor = UIColor.black
        textViewCaption.textAlignment = NSTextAlignment.center

        textViewCaption.textColor = UIColor.white
        textViewCaption.font = UIFont.systemFont(ofSize: 17.0)
        textViewCaption.isEditable = false
        textViewCaption.isSelectable = true
        textViewCaption.dataDetectorTypes = .link

        if let p = media  {
            let htmlData = NSString(string:String(format: "<HTML><body><font size='4'>%@</font></body></HTML>", p.caption)).data(using: String.Encoding.unicode.rawValue)

            let attributedString = try! NSAttributedString(data: htmlData!, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
            textViewCaption.attributedText = attributedString
        }

        self.addSubview(textViewCaption)  

这是检查链接的委托(delegate)调用:

public func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {

            if #available(iOS 10.0, *) {
                UIApplication.shared.open(URL, options: [:])
            } else {
                UIApplication.shared.openURL(URL)
            }

        return true
    }  

注意:所有这些都在自定义 View 中。

这是回应:

enter image description here

所以我这里有两个问题:

  1. Text not shown completely.
  2. Link not clickable.

最佳答案

使用 http 而不是 https 的链接默认被阻止。您需要在您的 info.plist

中添加一个异常(exception)
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>gregoryadunbar.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
        </dict>
    </dict>
</dict>

或允许所有不安全的链接

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

此外,还有更简单的方法可以将可点击链接添加到属性字符串,而不是构造一个 HTML 文档,您可以这样做

let text = "your full string including link gregoryadunbar.com"
let range = text.range(of: "gregoryadunbar.com")

let attributedText = NSMutableAttributedString(string: text)
attributedText.addAttribute(.link, value: "http://gregoryadunbar.com", range: range)
attributedText.addAttribute(.font, value: someFont, range: Range(location: 0, length: text.characters.count))
attributedText.addAttribute(.foregroundColor, value: UIColor.white range: Range(location: 0, length: text.characters.count))

如果你不设置 UITextView 的委托(delegate),这应该默认工作

如果您的链接包含在 a href 标签中,您可以像这样去除链接中的所有 HTML 标签:

let link = "A. Dunbar <a href=\"http://gregoryadunbar.com\" rel=\"nofollow\">gregoryadunbar.com</a>\n"
let linkWithoutHtmlTags = link.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil) // gregoryadunbar.com 

关于ios - UITextView href 链接不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48720722/

相关文章:

ios - 键值观察级联

iphone - NSPredicate 过滤器

ios - 如何在 Swift 4 中以编程方式将 IBAction 添加到按钮?

javascript - WKWebview从iPhone的文档目录加载外部脚本JS

objective-c - 分组的 UITableView 单元格中的 UITextView

iphone - 在UITableView内部实现动态增长的TextView

ios - Swift 中奇怪的 UInt64 行为

ios - 请求被服务委托(delegate) (SBMainWorkspace) 拒绝

ios - 无法将类型 '[PHAsset]' 的值分配给类型 'UIImageView!'

ios - 将 UITextView 光标位置设置为文本结尾