UITextView and UITextViewDelegate example in Swift

In this blog post I am going to share with you how to create and customize UITextView. We will learn how to create UITextView using Xcode Interface Designer as well as how to create and customize UITextView programatically in our Swift code.

Some of the things that I will cover in these four videos are:

  • Create UITextView
  • Customize UITextView font size, style and colour as well as background colour
  • Learn how to make UITextView editable and how to disable editing
  • Make corners of UITextView rounded
  • Make UITextView detect Phone number or a Link so that user can tap on it
  • Handle UITextViewDelegate methods to learn when user started, finished editing text or when user tapped on a web site address.
  • And more

Create and Customize UITextView using Xcode Interface Designer 

Customize UITextView using Swift code

Handle UITextViewDelegate functions

Little Practical Example using UITextView

Download source code to the “Little Practical Example” from GitHub

Some of the code snippets used in above videos: 

 

// Make current ViewController respond to UITextViewDelegate events 
myTextView.delegate = self
 
        // Update UITextView content
        myTextView.text = "In this video we are going to edit UITextView from our Swift code. www.swiftdeveloperblog.com"

        // Change UITextView background colour
        myTextView.backgroundColor = UIColor.yellowColor()

        // User RGB colour
        myTextView.backgroundColor = UIColor(red: 39/255, green: 53/255, blue: 182/255, alpha: 1)
        
        // Update UITextView font size and colour
        myTextView.font = UIFont.systemFontOfSize(20)
        myTextView.textColor = UIColor.whiteColor()
        
        myTextView.font = UIFont.boldSystemFontOfSize(20)
        myTextView.font = UIFont(name: "Verdana", size: 17)
        
       // Make UITextView Editable
       myTextView.editable = true

       // Capitalize all characters user types
       myTextView.autocapitalizationType = UITextAutocapitalizationType.AllCharacters
        
        // Make web links clickable
          myTextView.selectable = true
          myTextView.editable = false
          myTextView.dataDetectorTypes = UIDataDetectorTypes.Link
        
       // Make UITextView corners rounded
        myTextView.layer.cornerRadius = 10
 
        // Enable auto-correction and Spellcheck 
        myTextView.autocorrectionType = UITextAutocorrectionType.Yes
        myTextView.spellCheckingType = UITextSpellCheckingType.Yes
       // myTextView.autocapitalizationType = UITextAutocapitalizationType.None
        
        // Add Tap gesture to be able to dismiss keyboard when user taps away
        // You will need to declare a new function "tappedAwayFunction"
        let myGesture = UITapGestureRecognizer(target: self, action: "tappedAwayFunction:")
        self.view.addGestureRecognizer(myGesture)

I hope this tutorial was helpful to you 🙂

Happy coding!

Sergey