UITextView href link 不工作
UITextView href link not working
这是要显示的文本:
© Gregory A. Dunbar <a href=\"http://gregoryadunbar.com\" rel=\"nofollow\">gregoryadunbar.com</a>\n
这是我用来显示 UITextView 和触发器的代码 link :
这里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)
这是检查 link 的委托调用:
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
}
注意:所有这些都在自定义视图中。
这是回应:
所以我这里有两个问题:
- Text not shown completely.
- Link not clickable.
默认情况下会阻止带有 http
而不是 https
的链接。您需要在 info.plist
中添加例外
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>gregoryadunbar.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
或允许所有不安全的 links
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
此外,还有更简单的方法可以将可点击的 link 添加到属性字符串,而不是构建 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
的委托,这应该默认工作
如果您的 link 包含在 a href
标签中,您可以像这样去除 link 中的所有 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
这是要显示的文本:
© Gregory A. Dunbar <a href=\"http://gregoryadunbar.com\" rel=\"nofollow\">gregoryadunbar.com</a>\n
这是我用来显示 UITextView 和触发器的代码 link :
这里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)
这是检查 link 的委托调用:
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
}
注意:所有这些都在自定义视图中。
这是回应:
所以我这里有两个问题:
- Text not shown completely.
- Link not clickable.
默认情况下会阻止带有 http
而不是 https
的链接。您需要在 info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>gregoryadunbar.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
或允许所有不安全的 links
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
此外,还有更简单的方法可以将可点击的 link 添加到属性字符串,而不是构建 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
的委托,这应该默认工作
如果您的 link 包含在 a href
标签中,您可以像这样去除 link 中的所有 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