I've been looking into a solution to getting clickable links working. I can get this working when using UITextView + NSAttributedString but it just doesn't autolayout properly when it's a UITableViewCell.
Now I've added the TTTAttributedLabel to my project and it styles the views just perfectly. The links also turn blue and are underlined.
However clicking them does nothing. I did implement TTTAttributedLabelDelegate on my controller, made the label in the storyboard implement MyLabel (Which just extends TTTAttributedLabel and has the delegate options since I want them to fire inside the same function). For now I've set the controller to be the delegate I was thinking it might not work pointing to itself.
But none of these functions get fired, I got breakpoints and logs in it.
I Implemented didSelectLinkWithUrl and 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")
}
Outlet
@IBOutlet weak var content: MyLabel!
MyLabel
import UIKit import 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")
}
Anyone know what I could be missing?
Update
I found out that just pasting in an url f/e http://example.com becomes active and is actually clickable and the didSelectLinkWithUrl becomes clickable, allthough I need an attributed string and it's based on a HTML String.
NSTextCheckingResult.CheckingType.link.rawValue
– aehlke