Author Topic: How can I add placeholder text to a text view?  (Read 16028 times)

kjmcneish

  • Administrator
  • *****
  • Posts: 719
    • View Profile
How can I add placeholder text to a text view?
« on: May 29, 2014, 03:44:12 PM »
Although iOS text fields have a placeholder attribute, text views do not. To create the placeholder effect, you can dynamically add a label to the text view at run time, and then show and hide it.

Here is a Swift extension that allows you to do this (check out the implementation steps at the end of this post):

Code: [Select]
extension UITextView: UITextViewDelegate {
   
    // Placeholder text
    var placeholder: String? {
       
        get {
            // Get the placeholder text from the label
            var placeholderText: String?
           
            if let placeHolderLabel = self.viewWithTag(100) as? UILabel {
                placeholderText = placeHolderLabel.text
            }
            return placeholderText
        }
       
        set {
            // Store the placeholder text in the label
            var placeHolderLabel = self.viewWithTag(100) as UILabel?
            if placeHolderLabel == nil {
                // Add placeholder label to text view
                self.addPlaceholderLabel(newValue!)
            }
            else {
                placeHolderLabel?.text = newValue
                placeHolderLabel?.sizeToFit()
            }
        }
    }
   
    // Hide the placeholder label if there is no text
    // in the text viewotherwise, show the label
    public func textViewDidChange(textView: UITextView) {
       
        var placeHolderLabel = self.viewWithTag(100)

        if !self.hasText() {
            // Get the placeholder label
            placeHolderLabel?.hidden = false
        }
        else {
            placeHolderLabel?.hidden = true
        }
    }
   
    // Add a placeholder label to the text view
    func addPlaceholderLabel(placeholderText: String) {
       
        // Create the label and set its properties
        var placeholderLabel = UILabel()
        placeholderLabel.text = placeholderText
        placeholderLabel.sizeToFit()
        placeholderLabel.frame.origin.x = 5.0
        placeholderLabel.frame.origin.y = 5.0
        placeholderLabel.font = self.font
        placeholderLabel.textColor = UIColor.lightGrayColor()
        placeholderLabel.tag = 100
       
        // Hide the label if there is text in the text view
        placeholderLabel.hidden = (countElements(self.text) > 0)
       
        self.addSubview(placeholderLabel)
        self.delegate = self;
    }
   
}

This extension adds a placeholder property to your text views that you use to specify the placeholder text. When you set the placeholder text property, a label is added to your text views that appears when there is no text in the text view, and disappears when there is text.

To implement this extension in your project:

1. Add the extension to your project.

2. In the viewDidLoad method of the view controller that contains the text view, add code to set the placeholder text. For example:

Code: [Select]
self.textView.placeholder = "Enter your last name"
All the best!
Kevin McNeish
Author of "Learn to Code in Swift": https://itunes.apple.com/us/book/learn-to-code-in-swift/id942956811?mt=11
« Last Edit: July 19, 2015, 04:24:30 PM by kjmcneish »