Rob
Rob

Reputation: 629

Best way to handle textViewChanged in Swift 5.5

I'm trying to add attributed strings to some text in a UITextView on text change in Swift 5.5

What's the best approach, a UIView or a UIViewController?

Sample code would really help me out as I can't seem to get either approach to work atm.

Thanks

Upvotes: 0

Views: 51

Answers (1)

Smit Gajera
Smit Gajera

Reputation: 1039

class ViewController: UIViewController, UITextViewDelegate { //If your class is not conforms to the UITextViewDelegate protocol you will not be able to set it as delegate to UITextView

    @IBOutlet weak var bodyText: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()
        bodyText.delegate = self //Without setting the delegate you won't be able to track UITextView events
    }

    func textViewDidChange(_ textView: UITextView) { //Handle the text changes here
        print(textView.text); //the textView parameter is the textView where text was changed
    }
}

@Rob Hope this boilerplate works for you :)

Upvotes: 1

Related Questions