gpt4 book ai didi

ios - UITextView href 链接不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 07:04:52 24 4
gpt4 key购买 nike

这是要显示的文本:

© 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com