UITextView中的超链接不可单击。我在这里阅读了其他答案,但我不知道出了什么问题。 在我的情节提要中,我选择了“数据检测器”下的“链接”。启用了用户交互。 UITextView被委托给情节提要中的视图。
链接为https:
info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
shouldInteractWith永远不会被调用。
class AboutViewController: UIViewController, UITextViewDelegate {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.contentTextView.attributedText = self.pageDetails.content.htmlAttributedWithFamily(family: "SFProText-Regular", size: 10.0, color: UIColor.contentWhite)
}
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
print("Link Selected!")
return true
}
}
extension String {
func htmlAttributedWithFamily(family: String?, size: CGFloat, color: UIColor) -> NSAttributedString? {
do {
let htmlCSSString = "<style>" +
"html *" +
"{" +
"font-size: \(size)pt !important;" +
"color: #\(color.hexString!) !important;" +
"font-family: \(family ?? "Helvetica"), Helvetica !important;" +
"line-height:1.5" +
"}</style> \(self)"
guard let data = htmlCSSString.data(using: String.Encoding.utf8) else {
return nil
}
return try NSAttributedString(data: data,
options: [.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
} catch {
print("error: ", error)
return nil
}
}
}