UITextПросмотреть, как сделать гиперссылки кликабельными в атрибутивной строке - PullRequest
0 голосов
/ 12 декабря 2018

Гиперссылки внутри 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
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...