Os links TTTAttributedLabel estão sendo estilizados, mas não clicáveis
Estive procurando uma solução para fazer funcionar os links clicáveis. Eu posso fazer isso funcionar ao usar UITextView + NSAttributedString, mas ele simplesmente não realiza o auto-pagamento corretamente quando é um UITableViewCell.
Agora adicionei o TTTAttributedLabel ao meu projeto e estiliza as visualizações perfeitamente. Os links também ficam azuis e estão sublinhados.
No entanto, clicar neles não faz nada. Eu implementei TTTAttributedLabelDelegate no meu controlador, fiz com que o rótulo no storyboard implementasse MyLabel (que apenas estende o TTTAttributedLabel e tem as opções de delegação, pois eu quero que elas disparem dentro da mesma função). Por enquanto, eu configurei o controlador para ser o delegado que estava pensando que talvez não funcionasse apontando para si mesmo.
Mas nenhuma dessas funções é acionada, eu tenho pontos de interrupção e logs.
Implementei didSelectLinkWithUrl e didLongPressLinkWithUrl.
func attributedLabel(label: TTTAttributedLabel!, didSelectLinkWithURL url: NSURL!) {
Debug.log("link clicked")
}
func attributedLabel(label: TTTAttributedLabel!, didLongPressLinkWithURL url: NSURL!, atPoint point: CGPoint) {
Debug.log("link long clicked")
}
Saída
@IBOutlet weak var content: MyLabel!
MyLabel
importar UIKit importar TTTAttributedLabel
class MyLabel : TTTAttributedLabel, TTTAttributedLabelDelegate {
override func didMoveToSuperview() {
if (self.delegate == nil) {
self.delegate = self
}
self.enabledTextCheckingTypes = NSTextCheckingType.Link.rawValue
self.userInteractionEnabled = true
}
func attributedLabel(label: TTTAttributedLabel!, didSelectLinkWithURL url: NSURL!) {
Debug.log("link clicked")
}
func attributedLabel(label: TTTAttributedLabel!, didLongPressLinkWithURL url: NSURL!, atPoint point: CGPoint) {
Debug.log("link long clicked")
}
Alguém sabe o que eu poderia estar perdendo?
Atualizar
Eu descobri que apenas colando em um URL f / ehttp://example.com fica ativo e é realmente clicável e o didSelectLinkWithUrl se torna clicável, embora eu precise de uma sequência atribuída e seja baseada em uma String HTML.