Lance Samaria
Lance Samaria

Reputation: 19622

Swift iOS -NSMutableAttributedString Not Presenting ViewController?

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
    }
}

Upvotes: 0

Views: 157

Answers (2)

Lance Samaria
Lance Samaria

Reputation: 19622

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 {

}

Upvotes: 0

Bhavi Lad
Bhavi Lad

Reputation: 247

I think you forget to instantiate a view controller using its Storyboard name

here "Main" is storyboard name.

let storyboard = UIStoryboard(name: "Main", bundle: nil)

let secondVC = storyboard.instantiateViewController(withIdentifier: "SecondController") as! SecondController

So final code will be like,

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondVC = storyboard.instantiateViewController(withIdentifier: "SecondController") as! SecondController

let navVC = UINavigationController(rootViewController: secondVC)
present(navVC, animated: true, completion: nil)

Upvotes: 1

Related Questions