I'm adding a NSMutableAttributedString to a textView and I when I click the tappableString I want to present a new vc. The vc isn't being presented and I cannot figure out where I'm going wrong at.
I set the textView delegate and use the textView's ...shouldInteractWithURL
and inside there I check to see that the url's absolute string matches the NSLinkAttributeName's
value which is "doSomething" but the vc isn't appearing.
Where am I going wrong at?
class FirstController: UIViewController, UITextViewDelegate {
fileprivate let textView: UITextView = {
let textView = UITextView()
textView.translatesAutoresizingMaskIntoConstraints = false
textView.isEditable = false
textView.isSelectable = true
textView.textAlignment = .center
textView.isScrollEnabled = true
return textView
}()
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
configureTextView()
}
override func viewDidLayoutSubviews() {
textView.setContentOffset(.zero, animated: false)
}
func configureTextView(){
//textView anchors are set
let firstPlainSent = "Read this first.\n"
let secondPlainSent = "Read this second.\n"
plainSentAttr = [NSFontAttributeName: UIFont(name: "Helvetica", size: 17)!, NSForegroundColorAttributeName: UIColor.black]
let firstSent = NSAttributedString(string: firstPlainSent, attributes: plainSentAttr)
let secondSent = NSAttributedString(string: secondPlainSent, attributes: plainSentAttr)
let mutableAttributedString = NSMutableAttributedString()
mutableAttributedString.append(firstSent)
mutableAttributedString.append(secondSent)
let tappableString = NSMutableAttributedString(string: "Click here to present the SecondVC\n\n")
tappableString.addAttribute(NSFontAttributeName, value: UIFont(name: "Helvetica", size: 17)!, range: NSMakeRange(0, (tappableString.length)))
tappableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blue, range: NSMakeRange(0, (tappableString.length)))
tappableString.addAttribute(NSUnderlineStyleAttributeName, value: 1, range: NSMakeRange(0,tappableString.length))
tappableString.addAttribute(NSUnderlineColorAttributeName, value: UIColor.blue, range: NSMakeRange(0, tappableString.length))
tappableString.addAttribute(NSLinkAttributeName, value: "doSomething", range: NSMakeRange(0, (tappableString.length)))
mutableAttributedString.append(tappableString)
textView.attributedText = mutableAttributedString
}
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
// I also tried URL.scheme
if URL.absoluteString == "doSomething"{
let secondVC = SecondController()
let navVC = UINavigationController(rootViewController: secondVC)
present(navVC, animated: true, completion: nil)
return false
}
return true
}
}
The problem was I was using the older method signature of the textView's delegate method:
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool{
}
Once I used autocomplete and the newer method signature appeared everything worked fine:
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
}